Skip to content

Commit

Permalink
feat: expand attribute scheme to allow combining meshdata
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Sep 10, 2021
1 parent 89d45ce commit b07c797
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.rendering.assets.mesh;

import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.terasology.joml.test.VectorAssert;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StandardMeshDataTest {

@Test
public void testCombineMeshes() {
StandardMeshData m1 = new StandardMeshData();
m1.position.put(new Vector3f(10f, 10f, 10f));
m1.position.put(new Vector3f(15f, 20f, 10f));
m1.position.put(new Vector3f(10f, 30f, 10f));

StandardMeshData m2 = new StandardMeshData();
m2.position.put(new Vector3f(10f, 10f, 10f));
m2.position.put(new Vector3f(15f, 20f, 10f));
m2.position.put(new Vector3f(10f, 30f, 10f));

m1.combine(new Matrix4f(), m2);

assertEquals(m1.position.getPosition(), 6);
assertEquals(m1.normal.getPosition(), 0);
assertEquals(m1.uv0.getPosition(), 0);
assertEquals(m1.uv1.getPosition(), 0);
assertEquals(m1.color0.getPosition(), 0);

VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f);
VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f);
VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f);

VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f);
VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f);
VectorAssert.assertEquals(m1.position.get(5, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package org.terasology.engine.rendering.assets.mesh;

import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector2fc;
import org.joml.Vector3f;
Expand Down Expand Up @@ -123,6 +124,37 @@ public void reallocate(int numVerts, int numIndices) {
indices.allocateElements(numIndices);
}


public void combine(Matrix4f transform, StandardMeshData data) {
combine(transform, this.position.getPosition(), data);
}

public void combine(Matrix4f transform, int offset, StandardMeshData data) {
if (!position.isEmpty() || !data.position.isEmpty()) {
position.setPosition(offset);
Vector3f temp = new Vector3f();
for (int i = 0; i < data.position.elements(); i++) {
this.position.put(transform.transformPosition(data.position.get(i, temp)));
}
}
if (!normal.isEmpty() || !data.normal.isEmpty()) {
VertexAttributeBinding.copy(data.normal, offset, this.normal, new Vector3f());
}
if (!uv0.isEmpty() || !data.uv0.isEmpty()) {
VertexAttributeBinding.copy(data.uv0, offset, this.uv0, new Vector2f());
}
if (!uv1.isEmpty() || !data.uv1.isEmpty()) {
VertexAttributeBinding.copy(data.uv1, offset, this.uv1, new Vector2f());
}
if (!color0.isEmpty() || !data.color0.isEmpty()) {
VertexAttributeBinding.copy(data.color0, offset, this.color0, new Color());
}
if (!light0.isEmpty() || !data.light0.isEmpty()) {
VertexAttributeBinding.copy(data.light0, offset, this.light0, new Vector3f());
}
}


@Override
public VertexAttributeBinding<Vector3fc, Vector3f> positions() {
return position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public VertexAttributeBinding(VertexResource resource, int offset, VertexAttribu
this.attribute = attribute;
}


public int elements() {
return getResource().elements();
}

public boolean isEmpty() {
return getResource().isEmpty();
}

@Override
public void reserve(int vertCount) {
resource.reserveElements(vertCount);
Expand All @@ -32,6 +35,22 @@ public void allocate(int elements) {
resource.mark();
}

/**
*
* @param from data getting copied from
* @param position position to start copying to dest
* @param dest the destination to fill
* @param target a temporary object to transfer data between from and dest
* @param <T>
* @param <I>
*/
public static <T,I extends T> void copy(VertexAttributeBinding<T, I> from, int position, VertexAttributeBinding<T, I> dest, I target) {
dest.setPosition(position);
for (int i = 0; i < from.elements(); i++) {
dest.put(from.get(i, target));
}
}

/**
* write a value by the index.
*
Expand Down

0 comments on commit b07c797

Please sign in to comment.