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

chore: add test cases for IndexResource and address some problems found through test cases #4886

Merged
merged 5 commits into from
Sep 8, 2021
Merged
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,87 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

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

import org.junit.jupiter.api.Test;
import org.terasology.engine.rendering.assets.mesh.resource.IndexResource;

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

public class IndexResourceTest {
@Test
public void testPutIndexResource() {
IndexResource in = new IndexResource();
in.put(10);
in.put(11);
in.put(15);

assertEquals(3, in.indices());
in.writeBuffer(buffer -> {
assertEquals(3 * Integer.BYTES, buffer.limit());

assertEquals(10, buffer.getInt(0));
assertEquals(11, buffer.getInt(1 * Integer.BYTES));
assertEquals(15, buffer.getInt(2 * Integer.BYTES));
});
}

@Test
public void testRewind() {
IndexResource in = new IndexResource();
in.put(10);
in.put(11);
in.put(15);

assertEquals(3, in.indices());
in.writeBuffer(buffer -> {
assertEquals(3 * Integer.BYTES, buffer.limit());

assertEquals(10, buffer.getInt(0));
assertEquals(11, buffer.getInt(1 * Integer.BYTES));
assertEquals(15, buffer.getInt(2 * Integer.BYTES));
});

in.rewind();
in.put(20);
assertEquals(3, in.indices());
in.writeBuffer(buffer -> {
assertEquals(3 * Integer.BYTES, buffer.limit());

assertEquals(20, buffer.getInt(0));
assertEquals(11, buffer.getInt(1 * Integer.BYTES));
assertEquals(15, buffer.getInt(2 * Integer.BYTES));
});

}

@Test
public void testAllocate() {

IndexResource in = new IndexResource();
in.allocateElements(20); // re-allocate to 20 indices

in.put(100);
in.put(14);
in.put(10);

assertEquals(20, in.indices());
in.writeBuffer(buffer -> {
assertEquals(20 * Integer.BYTES, buffer.limit());

assertEquals(100, buffer.getInt(0));
assertEquals(14, buffer.getInt(1 * Integer.BYTES));
assertEquals(10, buffer.getInt(2 * Integer.BYTES));
});

in.allocateElements(4); // re-allocate to 4 indices
assertEquals(4, in.indices());
in.writeBuffer(buffer -> {
assertEquals(4 * Integer.BYTES, buffer.limit());

assertEquals(100, buffer.getInt(0));
assertEquals(14, buffer.getInt(1 * Integer.BYTES));
assertEquals(10, buffer.getInt(2 * Integer.BYTES));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@
*/
public class IndexResource extends BufferedResource {
public static final Logger logger = LoggerFactory.getLogger(IndexResource.class);
private int inIndices = 0;
private int posIndex = 0;

public IndexResource() {
super();
}

public int indices() {
return inIndices;
return this.inSize / Integer.BYTES;
}

public void copy(IndexResource resource) {
copyBuffer(resource);
this.inSize = resource.inSize;
this.inIndices = resource.indices();
}

public void reserveElements(int elements) {
Expand All @@ -36,16 +34,12 @@ public void reserveElements(int elements) {

public void rewind() {
posIndex = 0;
inIndices = 0;
}

public void put(int value) {
ensureCapacity((posIndex + 1) * Integer.BYTES);
buffer.putInt(posIndex * Integer.BYTES, value);
posIndex++;
if (posIndex > inIndices) {
inIndices = posIndex;
}
}

public void putAll(int value, int... values) {
Expand Down Expand Up @@ -77,10 +71,6 @@ public void put(int index, int value) {

@Override
public boolean isEmpty() {
return inIndices == 0;
return this.inSize == 0;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class OpenGLMesh extends Mesh implements OpenGLMeshBase {
private DrawingMode drawMode;
private AllocationType allocationType;

private VertexAttributeBinding<Vector3fc, Vector3f> positions;
private VertexAttributeBinding<Vector3fc, Vector3f> positions;

private VBOContext state = null;

Expand Down Expand Up @@ -79,7 +79,7 @@ public void render() {
if (!isDisposed()) {
updateState(state);
GL30.glBindVertexArray(disposalAction.vao);
if(this.indexCount == 0) {
if (this.indexCount == 0) {
GL30.glDrawArrays(drawMode.glCall, 0, positions.elements());
} else {
GL30.glDrawElements(drawMode.glCall, this.indexCount, GL_UNSIGNED_INT, 0);
Expand Down