Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expand attribute scheme to allow combining meshdata #4900

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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.jupiter.api.Test;
import org.terasology.joml.test.VectorAssert;
import org.terasology.nui.Color;

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

public class StandardMeshDataTest {

@Test
public void combineStandardMeshData() {
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);

}

@Test
public void combineStandardMeshDataWithTransform() {
StandardMeshData m1 = new StandardMeshData();
m1.position.put(new Vector3f(-1f, 1f, 0f));
m1.position.put(new Vector3f(1f, 1f, 0f));
m1.position.put(new Vector3f(1f, -1f, 0f));
m1.position.put(new Vector3f(-1f, -1f, 0f));

StandardMeshData m2 = new StandardMeshData();
m2.position.put(new Vector3f(-1f, 1f, 0f));
m2.position.put(new Vector3f(1f, 1f, 0f));
m2.position.put(new Vector3f(1f, -1f, 0f));
m2.position.put(new Vector3f(-1f, -1f, 0f));

m1.combine(new Matrix4f().translate(10f,0,0), m2);

Check warning on line 60 in engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java

View check run for this annotation

Terasology Jenkins.io / CheckStyle

WhitespaceAfterCheck

NORMAL: ',' is not followed by whitespace.
Raw output
<p>Since Checkstyle 3.0</p><p> Checks that a token is followed by whitespace. </p>

Check warning on line 60 in engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java

View check run for this annotation

Terasology Jenkins.io / CheckStyle

WhitespaceAfterCheck

NORMAL: ',' is not followed by whitespace.
Raw output
<p>Since Checkstyle 3.0</p><p> Checks that a token is followed by whitespace. </p>

VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(-1f, 1f, 0f), .001f);
VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(1f, 1f, 0f), .001f);
VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(1f, -1f, 0f), .001f);
VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(-1f, -1f, 0f), .001f);

VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(9f, 1f, 0), .001f);
VectorAssert.assertEquals(m1.position.get(5, new Vector3f()), new Vector3f(11f, 1f, 0), .001f);
VectorAssert.assertEquals(m1.position.get(6, new Vector3f()), new Vector3f(11f, -1f, 0), .001f);
VectorAssert.assertEquals(m1.position.get(7, new Vector3f()), new Vector3f(9f, -1f, 0), .001f);
}

@Test
public void secondMeshWithColorCombineStandardMeshData() {
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));
m2.color0.put(Color.blue);
m2.color0.put(Color.blue);
m2.color0.put(Color.blue);

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

assertEquals(m1.position.getPosition(), 6);
assertEquals(m1.color0.getPosition(), 6);
assertEquals(m1.normal.getPosition(), 0);
assertEquals(m1.uv0.getPosition(), 0);
assertEquals(m1.uv1.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);
assertEquals(m1.color0.get(0, new Color()), new Color(Color.transparent));
assertEquals(m1.color0.get(1, new Color()), new Color(Color.transparent));
assertEquals(m1.color0.get(2, new Color()), new Color(Color.transparent));

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);
assertEquals(m1.color0.get(3, new Color()), new Color(Color.blue));
assertEquals(m1.color0.get(4, new Color()), new Color(Color.blue));
assertEquals(m1.color0.get(5, new Color()), new Color(Color.blue));
}
}
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,39 @@ public void reallocate(int numVerts, int numIndices) {
indices.allocateElements(numIndices);
}


/**
* append mesh data to current mesh
* @param transform transformation to apply to target
* @param target target mesh
*/
public void combine(Matrix4f transform, StandardMeshData target) {
int start = this.position.getPosition();
if (!position.isEmpty() || !target.position.isEmpty()) {
position.setPosition(start);
Vector3f temp = new Vector3f();
for (int i = 0; i < target.position.elements(); i++) {
this.position.put(transform.transformPosition(target.position.get(i, temp)));
}
}
if (!normal.isEmpty() || !target.normal.isEmpty()) {
VertexAttributeBinding.copy(target.normal, start, this.normal, new Vector3f());
}
if (!uv0.isEmpty() || !target.uv0.isEmpty()) {
VertexAttributeBinding.copy(target.uv0, start, this.uv0, new Vector2f());
}
if (!uv1.isEmpty() || !target.uv1.isEmpty()) {
VertexAttributeBinding.copy(target.uv1, start, this.uv1, new Vector2f());
}
if (!color0.isEmpty() || !target.color0.isEmpty()) {
VertexAttributeBinding.copy(target.color0, start, this.color0, new Color());
}
if (!light0.isEmpty() || !target.light0.isEmpty()) {
VertexAttributeBinding.copy(target.light0, start, 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 @@
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 @@
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) {

Check warning on line 47 in engine/src/main/java/org/terasology/engine/rendering/assets/mesh/resource/VertexAttributeBinding.java

View check run for this annotation

Terasology Jenkins.io / CheckStyle

WhitespaceAfterCheck

NORMAL: ',' is not followed by whitespace.
Raw output
<p>Since Checkstyle 3.0</p><p> Checks that a token is followed by whitespace. </p>
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
Loading