Skip to content

Commit b07c797

Browse files
committed
feat: expand attribute scheme to allow combining meshdata
1 parent 89d45ce commit b07c797

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 The Terasology Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package org.terasology.engine.rendering.assets.mesh;
5+
6+
import org.joml.Matrix4f;
7+
import org.joml.Vector3f;
8+
import org.junit.Assert;
9+
import org.junit.jupiter.api.Test;
10+
import org.terasology.joml.test.VectorAssert;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class StandardMeshDataTest {
15+
16+
@Test
17+
public void testCombineMeshes() {
18+
StandardMeshData m1 = new StandardMeshData();
19+
m1.position.put(new Vector3f(10f, 10f, 10f));
20+
m1.position.put(new Vector3f(15f, 20f, 10f));
21+
m1.position.put(new Vector3f(10f, 30f, 10f));
22+
23+
StandardMeshData m2 = new StandardMeshData();
24+
m2.position.put(new Vector3f(10f, 10f, 10f));
25+
m2.position.put(new Vector3f(15f, 20f, 10f));
26+
m2.position.put(new Vector3f(10f, 30f, 10f));
27+
28+
m1.combine(new Matrix4f(), m2);
29+
30+
assertEquals(m1.position.getPosition(), 6);
31+
assertEquals(m1.normal.getPosition(), 0);
32+
assertEquals(m1.uv0.getPosition(), 0);
33+
assertEquals(m1.uv1.getPosition(), 0);
34+
assertEquals(m1.color0.getPosition(), 0);
35+
36+
VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f);
37+
VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f);
38+
VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f);
39+
40+
VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f);
41+
VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f);
42+
VectorAssert.assertEquals(m1.position.get(5, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f);
43+
44+
}
45+
}

engine/src/main/java/org/terasology/engine/rendering/assets/mesh/StandardMeshData.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

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

6+
import org.joml.Matrix4f;
67
import org.joml.Vector2f;
78
import org.joml.Vector2fc;
89
import org.joml.Vector3f;
@@ -123,6 +124,37 @@ public void reallocate(int numVerts, int numIndices) {
123124
indices.allocateElements(numIndices);
124125
}
125126

127+
128+
public void combine(Matrix4f transform, StandardMeshData data) {
129+
combine(transform, this.position.getPosition(), data);
130+
}
131+
132+
public void combine(Matrix4f transform, int offset, StandardMeshData data) {
133+
if (!position.isEmpty() || !data.position.isEmpty()) {
134+
position.setPosition(offset);
135+
Vector3f temp = new Vector3f();
136+
for (int i = 0; i < data.position.elements(); i++) {
137+
this.position.put(transform.transformPosition(data.position.get(i, temp)));
138+
}
139+
}
140+
if (!normal.isEmpty() || !data.normal.isEmpty()) {
141+
VertexAttributeBinding.copy(data.normal, offset, this.normal, new Vector3f());
142+
}
143+
if (!uv0.isEmpty() || !data.uv0.isEmpty()) {
144+
VertexAttributeBinding.copy(data.uv0, offset, this.uv0, new Vector2f());
145+
}
146+
if (!uv1.isEmpty() || !data.uv1.isEmpty()) {
147+
VertexAttributeBinding.copy(data.uv1, offset, this.uv1, new Vector2f());
148+
}
149+
if (!color0.isEmpty() || !data.color0.isEmpty()) {
150+
VertexAttributeBinding.copy(data.color0, offset, this.color0, new Color());
151+
}
152+
if (!light0.isEmpty() || !data.light0.isEmpty()) {
153+
VertexAttributeBinding.copy(data.light0, offset, this.light0, new Vector3f());
154+
}
155+
}
156+
157+
126158
@Override
127159
public VertexAttributeBinding<Vector3fc, Vector3f> positions() {
128160
return position;

engine/src/main/java/org/terasology/engine/rendering/assets/mesh/resource/VertexAttributeBinding.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public VertexAttributeBinding(VertexResource resource, int offset, VertexAttribu
1616
this.attribute = attribute;
1717
}
1818

19-
2019
public int elements() {
2120
return getResource().elements();
2221
}
2322

23+
public boolean isEmpty() {
24+
return getResource().isEmpty();
25+
}
26+
2427
@Override
2528
public void reserve(int vertCount) {
2629
resource.reserveElements(vertCount);
@@ -32,6 +35,22 @@ public void allocate(int elements) {
3235
resource.mark();
3336
}
3437

38+
/**
39+
*
40+
* @param from data getting copied from
41+
* @param position position to start copying to dest
42+
* @param dest the destination to fill
43+
* @param target a temporary object to transfer data between from and dest
44+
* @param <T>
45+
* @param <I>
46+
*/
47+
public static <T,I extends T> void copy(VertexAttributeBinding<T, I> from, int position, VertexAttributeBinding<T, I> dest, I target) {
48+
dest.setPosition(position);
49+
for (int i = 0; i < from.elements(); i++) {
50+
dest.put(from.get(i, target));
51+
}
52+
}
53+
3554
/**
3655
* write a value by the index.
3756
*

0 commit comments

Comments
 (0)