use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.
the class AtlasFormat method process.
private void process(GridDefinition grid, Texture texture, Vector2i size, Map<Name, SubtextureData> out) {
if (grid.getTileSize() == null) {
logger.error("Bad grid definition - missing mandatory property tileSize");
return;
}
if (grid.getGridDimensions() == null) {
logger.error("Bad grid definition - missing mandatory property gridDimensions");
return;
}
Vector2f offset = new Vector2f(0, 0);
if (grid.getGridOffset() != null) {
offset.set((float) grid.getGridOffset().x / size.x, (float) grid.getGridOffset().y / size.y);
}
Vector2f tileSize = new Vector2f((float) grid.getTileSize().x / size.x, (float) grid.getTileSize().y / size.y);
int tileX = 0;
int tileY = 0;
for (String name : grid.getTileNames()) {
if (!name.isEmpty()) {
Vector2f pos = new Vector2f(offset);
pos.x += tileX * tileSize.x;
pos.y += tileY * tileSize.y;
Rect2f tileLocation = Rect2f.createFromMinAndSize(offset.x + tileX * tileSize.x, offset.y + tileY * tileSize.y, tileSize.x, tileSize.y);
out.put(new Name(name), new SubtextureData(texture, tileLocation));
}
tileX++;
if (tileX == grid.getGridDimensions().x) {
tileX = 0;
tileY++;
}
}
}
use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.
the class AtlasFormat method process.
private void process(FreeformDefinition freeform, Texture texture, Vector2i size, Map<Name, SubtextureData> out) {
if (freeform.getName() == null || freeform.getName().isEmpty()) {
logger.error("Bad subimage definition - missing mandatory property name");
return;
}
if (freeform.getMin() == null) {
logger.error("Bad subimage definition '{}' - missing mandatory property min", freeform.getName());
return;
}
if (freeform.getSize() == null && freeform.getMax() == null) {
logger.error("Bad subimage definition '{}' - requires one of max or size", freeform.getName());
return;
}
Vector2f min = new Vector2f((float) freeform.getMin().x / size.x, (float) freeform.getMin().y / size.y);
if (freeform.getSize() != null) {
Vector2f itemSize = new Vector2f((float) freeform.getSize().x / size.x, (float) freeform.getSize().y / size.y);
out.put(new Name(freeform.getName()), new SubtextureData(texture, Rect2f.createFromMinAndSize(min, itemSize)));
} else if (freeform.getMax() != null) {
Vector2f max = new Vector2f((float) freeform.getMax().x / size.x, (float) freeform.getMax().y / size.y);
out.put(new Name(freeform.getName()), new SubtextureData(texture, Rect2f.createFromMinAndMax(min, max)));
}
}
use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.
the class ObjMeshFormat method load.
@Override
public MeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputs.get(0).openStream()))) {
List<Vector3f> rawVertices = Lists.newArrayList();
List<Vector3f> rawNormals = Lists.newArrayList();
List<Vector2f> rawTexCoords = Lists.newArrayList();
List<Vector3i[]> rawIndices = Lists.newArrayList();
// Gather data
readMeshData(reader, rawVertices, rawNormals, rawTexCoords, rawIndices);
// Determine face format;
if (rawIndices.size() == 0) {
throw new IOException("No index data");
}
MeshData data = processData(rawVertices, rawNormals, rawTexCoords, rawIndices);
if (data.getVertices() == null) {
throw new IOException("No vertices define");
}
if (!data.getNormals().isEmpty() && data.getNormals().size() != data.getVertices().size()) {
throw new IOException("The number of normals does not match the number of vertices.");
}
if (!data.getTexCoord0().isEmpty() && data.getTexCoord0().size() / 2 != data.getVertices().size() / 3) {
throw new IOException("The number of tex coords does not match the number of vertices.");
}
return data;
}
}
use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.
the class ObjMeshFormat method readMeshData.
private void readMeshData(BufferedReader reader, List<Vector3f> rawVertices, List<Vector3f> rawNormals, List<Vector2f> rawTexCoords, List<Vector3i[]> rawIndices) throws IOException {
String line = null;
int lineNum = 0;
try {
while ((line = reader.readLine()) != null) {
line = line.trim();
lineNum++;
if (line.isEmpty()) {
continue;
}
String[] prefixSplit = line.trim().split("\\s+", 2);
String prefix = prefixSplit[0];
// Comment
if ("#".equals(prefix)) {
continue;
}
if (prefixSplit.length < 2) {
throw new IOException(String.format("Incomplete statement"));
}
switch(prefix) {
// Object name
case "o":
// Just skip the name
break;
// Vertex position
case "v":
{
String[] floats = prefixSplit[1].trim().split("\\s+", 4);
if (floats.length != 3) {
throw new IOException("Bad statement");
}
rawVertices.add(new Vector3f(Float.parseFloat(floats[0]), Float.parseFloat(floats[1]), Float.parseFloat(floats[2])));
break;
}
// Vertex texture coords
case "vt":
{
String[] floats = prefixSplit[1].trim().split("\\s+", 4);
if (floats.length < 2 || floats.length > 3) {
throw new IOException("Bad statement");
}
// Need to flip v coord, apparently
rawTexCoords.add(new Vector2f(Float.parseFloat(floats[0]), Float.parseFloat(floats[1])));
break;
}
// Vertex normal
case "vn":
{
String[] floats = prefixSplit[1].trim().split("\\s+", 4);
if (floats.length != 3) {
throw new IOException("Bad statement");
}
rawNormals.add(new Vector3f(Float.parseFloat(floats[0]), Float.parseFloat(floats[1]), Float.parseFloat(floats[2])));
break;
}
// Material name (ignored)
case "usemtl":
break;
// Smoothing group (not supported)
case "s":
{
if (!"off".equals(prefixSplit[1]) && !"0".equals(prefixSplit[1])) {
logger.warn("Smoothing groups not supported in obj import yet");
}
break;
}
// Face (polygon)
case "f":
{
String[] elements = prefixSplit[1].trim().split("\\s+");
Vector3i[] result = new Vector3i[elements.length];
for (int i = 0; i < elements.length; ++i) {
String[] parts = elements[i].split("/", 4);
if (parts.length > 3) {
throw new IOException("Bad Statement");
}
result[i] = new Vector3i(Integer.parseInt(parts[0]), -1, -1);
if (parts.length > 1 && !parts[1].isEmpty()) {
result[i].y = Integer.parseInt(parts[1]);
}
if (parts.length > 2 && !parts[2].isEmpty()) {
result[i].z = Integer.parseInt(parts[2]);
}
}
rawIndices.add(result);
break;
}
default:
logger.warn("Skipping unsupported obj statement on line {}:\"{}\"", lineNum, line);
}
}
} catch (RuntimeException e) {
throw new IOException(String.format("Failed to process line %d:\"%s\"", lineNum, line), e);
}
}
use of org.terasology.math.geom.Vector2f in project Terasology by MovingBlocks.
the class Rect2fTypeHandler method deserialize.
@Override
public Rect2f deserialize(PersistedData data, DeserializationContext context) {
if (!data.isNull() && data.isValueMap()) {
PersistedDataMap map = data.getAsValueMap();
Vector2f min = context.deserializeAs(map.get(MIN_FIELD), Vector2f.class);
Vector2f size = context.deserializeAs(map.get(SIZE_FIELD), Vector2f.class);
return Rect2f.createFromMinAndSize(min, size);
}
return null;
}
Aggregations