use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class BlockSelectionRenderer method buildBlockMesh.
private static void buildBlockMesh(StandardMeshData mesh, float size, Rectanglef texRect, Colorc c) {
Vector3f pos = new Vector3f();
Vector3f norm = new Vector3f();
Vector2f texCoord = new Vector2f();
final float sizeHalf = size / 2;
int firstIndex = mesh.position.getPosition();
// top
mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, -sizeHalf));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, 1.0f, 0));
mesh.color0.put(c);
}
// left
mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, -sizeHalf));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.minY, texRect.minY));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(-1.0f, 0, 0));
mesh.color0.put(c);
}
// right
mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, -sizeHalf));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(1.0f, 0, 0));
mesh.color0.put(c);
}
// back
mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, -sizeHalf));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, 0, -1.0f));
mesh.color0.put(c);
}
// front
mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, sizeHalf, sizeHalf));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.minY, texRect.minY));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, 0, 1.0f));
mesh.color0.put(c);
}
// bottom
mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.zero().add(sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.zero().add(-sizeHalf, -sizeHalf, sizeHalf));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.minY));
mesh.uv0.put(texCoord.set(texRect.maxX, texRect.maxY));
mesh.uv0.put(texCoord.set(texRect.minX, texRect.maxY));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, -1, 0f));
mesh.color0.put(c);
}
int lastIndex = mesh.position.getPosition();
for (int i = firstIndex; i < lastIndex - 2; i += 4) {
mesh.indices.put(i);
mesh.indices.put(i + 1);
mesh.indices.put(i + 2);
mesh.indices.put(i + 2);
mesh.indices.put(i + 3);
mesh.indices.put(i);
}
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class OpenGLSkeletalMesh method doReload.
@Override
protected void doReload(SkeletalMeshData newData) {
try {
GameThread.synch(() -> {
this.data = newData;
if (this.disposalAction.vao == 0) {
this.disposalAction.vao = GL30.glGenVertexArrays();
this.disposalAction.vbo = GL30.glGenBuffers();
this.disposalAction.ebo = GL30.glGenBuffers();
}
// bind vertex array and buffer
GL30.glBindVertexArray(this.disposalAction.vao);
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, this.disposalAction.vbo);
GL30.glEnableVertexAttribArray(StandardMeshData.VERTEX_INDEX);
GL30.glVertexAttribPointer(StandardMeshData.VERTEX_INDEX, 3, GL30.GL_FLOAT, false, VERTEX_NORMAL_SIZE, 0);
GL30.glEnableVertexAttribArray(StandardMeshData.NORMAL_INDEX);
GL30.glVertexAttribPointer(StandardMeshData.NORMAL_INDEX, 3, GL30.GL_FLOAT, false, VERTEX_NORMAL_SIZE, VERTEX_SIZE);
GL30.glEnableVertexAttribArray(StandardMeshData.UV0_INDEX);
GL30.glVertexAttribPointer(StandardMeshData.UV0_INDEX, 2, GL30.GL_FLOAT, false, UV_SIZE, (long) VERTEX_NORMAL_SIZE * newData.getVertexCount());
int payloadSize = (UV_SIZE + VERTEX_SIZE + NORMAL_SIZE) * newData.getVertexCount();
ByteBuffer buffer = BufferUtils.createByteBuffer(payloadSize);
buffer.position(newData.getVertexCount() * VERTEX_NORMAL_SIZE);
for (Vector2f uv : newData.getUVs()) {
buffer.putFloat(uv.x);
buffer.putFloat(uv.y);
}
buffer.flip();
GL30.glBufferData(GL30.GL_ARRAY_BUFFER, buffer, GL30.GL_DYNAMIC_DRAW);
IntBuffer indexBuffer = BufferUtils.createIntBuffer(newData.getIndices().size());
indexBuffer.put(newData.getIndices().toArray());
indexBuffer.flip();
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, this.disposalAction.ebo);
GL30.glBufferData(GL30.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL30.GL_STATIC_DRAW);
GL30.glBindVertexArray(0);
});
} catch (InterruptedException e) {
logger.error("Failed to reload {}", getUrn(), e);
}
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class TessellatorHelper method addGUIQuadMesh.
public static void addGUIQuadMesh(Tessellator tessellator, Vector4f color, float sizeX, float sizeY) {
tessellator.resetParams();
tessellator.setColor(new Vector4f(color.x, color.y, color.z, color.w));
tessellator.setUseLighting(false);
tessellator.setUseNormals(false);
tessellator.addPoly(new Vector3f[] { new Vector3f(0, 0, 0), new Vector3f(sizeX, 0, 0), new Vector3f(sizeX, sizeY, 0), new Vector3f(0, sizeY, 0) }, new Vector2f[] { new Vector2f(0, 0), new Vector2f(1, 0), new Vector2f(1, 1), new Vector2f(0, 1) });
tessellator.setUseLighting(true);
tessellator.setUseNormals(true);
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class Voronoi method processCell.
private void processCell(int cellX, int cellY, Vector2f at, VoronoiResult[] results) {
long seed = (702395077 * (long) cellX + 915488749 * (long) cellY);
// Number of features
int count = poissonCount[(int) (seed >> 24)];
seed = incrementSeed(seed);
for (int point = 0; point < count; point++) {
long id = seed;
seed = incrementSeed(seed);
float x = (seed + 0.5f) / 4294967296.0f;
seed = incrementSeed(seed);
float y = (seed + 0.5f) / 4294967296.0f;
seed = incrementSeed(seed);
Vector2f innerPos = new Vector2f(x, y);
Vector2f delta = new Vector2f(cellX + innerPos.x - at.x, cellY + innerPos.y - at.y);
float dist = standardDistanceFunction(delta);
if (dist < results[results.length - 1].distance) {
int index = results.length - 1;
while (index > 0 && dist < results[index - 1].distance) {
index--;
}
for (int i = results.length - 1; i > index; i--) {
results[i] = results[i - 1];
}
results[index].distance = dist;
results[index].delta = new Vector2f(delta);
results[index].id = (int) id;
}
}
}
use of org.joml.Vector2f in project Terasology by MovingBlocks.
the class Voronoi method getClosestPoints.
/**
* @param at
* @param numPoints Should be ≤ 5. The number of points to return
* @return
*/
public VoronoiResult[] getClosestPoints(Vector2f at, int numPoints) {
VoronoiResult[] results = new VoronoiResult[numPoints];
for (VoronoiResult result : results) {
result.distance = Float.MAX_VALUE;
}
at.mul(DENSITY_ADJUSTMENT);
at.add(offset);
int cellX = Math.roundUsing(at.x, RoundingMode.FLOOR);
int cellY = Math.roundUsing(at.y, RoundingMode.FLOOR);
processCell(cellX, cellY, at, results);
Vector2f cellPos = new Vector2f(at);
cellPos.x -= cellX;
cellPos.y -= cellY;
Vector2f distMax = new Vector2f(standardDistanceFunction(new Vector2f(1 - cellPos.x, 0)), standardDistanceFunction(new Vector2f(0, 1 - cellPos.y)));
Vector2f distMin = new Vector2f(standardDistanceFunction(new Vector2f(cellPos.x, 0)), standardDistanceFunction(new Vector2f(0, cellPos.y)));
// Test near cells
if (distMin.x < results[results.length - 1].distance) {
processCell(cellX - 1, cellY, at, results);
}
if (distMin.y < results[results.length - 1].distance) {
processCell(cellX, cellY - 1, at, results);
}
if (distMax.x < results[results.length - 1].distance) {
processCell(cellX + 1, cellY, at, results);
}
if (distMax.y < results[results.length - 1].distance) {
processCell(cellX, cellY + 1, at, results);
}
// Test further cells
if (distMin.x + distMin.y < results[results.length - 1].distance) {
processCell(cellX - 1, cellY - 1, at, results);
}
if (distMax.x + distMax.y < results[results.length - 1].distance) {
processCell(cellX + 1, cellY + 1, at, results);
}
if (distMin.x + distMax.y < results[results.length - 1].distance) {
processCell(cellX - 1, cellY + 1, at, results);
}
if (distMax.x + distMin.y < results[results.length - 1].distance) {
processCell(cellX + 1, cellY - 1, at, results);
}
for (VoronoiResult result : results) {
result.delta.mul(INVERSE_DENSITY_ADJUSTMENT);
result.distance *= INVERSE_DENSITY_ADJUSTMENT * INVERSE_DENSITY_ADJUSTMENT;
}
return results;
}
Aggregations