use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class VertexGLAttributeTest method testVector4fBinding.
@Test
public void testVector4fBinding() {
VertexResourceBuilder builder = new VertexResourceBuilder();
VertexAttributeBinding<Vector4fc, Vector4f> a1 = builder.add(0, GLAttributes.VECTOR_4_F_VERTEX_ATTRIBUTE);
VertexResource resource = builder.build();
a1.put(new Vector4f(10, 150, -10, 12));
a1.put(new Vector4f(15.1f, 15.04f, 10, 12));
a1.put(new Vector4f(16f, 150, -10, 12));
assertEquals(3, a1.getPosition());
int stride = Float.BYTES * 4;
resource.writeBuffer(buffer -> {
assertEquals(3 * 4 * Float.BYTES, buffer.limit());
assertEquals(10, buffer.getFloat(Float.BYTES * 0), 0.001f);
assertEquals(150, buffer.getFloat(Float.BYTES * 1), 0.001f);
assertEquals(-10, buffer.getFloat(Float.BYTES * 2), 0.001f);
assertEquals(12, buffer.getFloat(Float.BYTES * 3), 0.001f);
assertEquals(15.1f, buffer.getFloat((stride) + Float.BYTES * 0), 0.001f);
assertEquals(15.04f, buffer.getFloat((stride) + Float.BYTES * 1), 0.001f);
assertEquals(10, buffer.getFloat((stride) + Float.BYTES * 2), 0.001f);
assertEquals(12, buffer.getFloat((stride) + Float.BYTES * 3), 0.001f);
assertEquals(16f, buffer.getFloat((stride * 2) + Float.BYTES * 0), 0.001f);
assertEquals(150f, buffer.getFloat((stride * 2) + Float.BYTES * 1), 0.001f);
assertEquals(-10, buffer.getFloat((stride * 2) + Float.BYTES * 2), 0.001f);
assertEquals(12, buffer.getFloat((stride * 2) + Float.BYTES * 3), 0.001f);
});
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class GLTFCommonFormat method loadVector4fList.
protected List<Vector4f> loadVector4fList(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
TFloatList floats = readFloatBuffer(semantic, gltfPrimitive, gltf, loadedBuffers);
List<Vector4f> vectors = Lists.newArrayListWithCapacity(floats.size() / 4);
for (int i = 0; i < floats.size(); i += 4) {
vectors.add(new Vector4f(floats.get(i), floats.get(i + 1), floats.get(i + 2), floats.get(i + 3)));
}
return vectors;
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class GLTFAttributeMapping method readVec4FBuffer.
public static void readVec4FBuffer(byte[] buffer, GLTFAccessor accessor, GLTFBufferView bufferView, VertexAttributeBinding<Vector4fc, Vector4f> mapping) {
if (accessor.getComponentType() != GLTFComponentType.FLOAT) {
return;
}
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, bufferView.getByteOffset() + accessor.getByteOffset(), bufferView.getByteLength() - accessor.getByteOffset());
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
int gap = 0;
if (bufferView.getByteStride() > 0) {
gap = bufferView.getByteStride() - accessor.getComponentType().getByteLength() * accessor.getType().getDimension();
}
Vector4f pos = new Vector4f();
if (byteBuffer.position() < byteBuffer.limit()) {
for (int i = 0; i < accessor.getType().getDimension(); i++) {
pos.setComponent(i, byteBuffer.getFloat());
}
mapping.put(pos);
}
while (byteBuffer.position() < byteBuffer.limit() - gap) {
byteBuffer.position(byteBuffer.position() + gap);
for (int i = 0; i < accessor.getType().getDimension(); i++) {
pos.setComponent(i, byteBuffer.getFloat());
}
mapping.put(pos);
}
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class LineRenderer method draw.
/**
* Draws a 2D line segment in OpenGL.
*
* @param x1 The X-coordinate of the segment's start point.
* @param y1 The Y-coordinate of the segment's start point.
* @param x2 The X-coordinate of the segment's end point.
* @param y2 The Y-coordinate of the segment's end point.
* @param width Thickness of the line in pixels.
* @param color The line color.
* @param background The background color. Ignored if alpha blending is used.
* @param alpha The alpha channel. If set to 0, alpha blending is not used.
* @see <a href="http://artgrammer.blogspot.de/2011/05/drawing-nearly-perfect-2d-line-segments.html">
* Drawing nearly perfect 2D line segments in OpenGL
* </a>
*/
public static void draw(float x1, float y1, float x2, float y2, float width, Colorc color, Colorc background, float alpha) {
if (lineMesh == null) {
lineMesh = Assets.generateAsset(lineMeshData, Mesh.class);
}
GL11.glDisable(GL11.GL_CULL_FACE);
float t = 0;
float r = 0;
float f = width - (int) width;
float a;
boolean alphaBlend = alpha > 0;
float cRed = color.rf();
float cGreen = color.gf();
float cBlue = color.bf();
float bRed = background.rf();
float bGreen = background.gf();
float bBlue = background.bf();
if (alphaBlend) {
a = alpha;
} else {
a = 1.f;
}
if (width >= 0.0 && width < 1.0) {
t = 0.05f;
r = 0.48f + 0.32f * f;
if (!alphaBlend) {
cRed += 0.88f * (1 - f);
cGreen += 0.88f * (1 - f);
cBlue += 0.88f * (1 - f);
if (cRed > 1.0f) {
cRed = 1.0f;
}
if (cGreen > 1.0f) {
cGreen = 1.0f;
}
if (cBlue > 1.0f) {
cBlue = 1.0f;
}
} else {
a *= f;
}
} else if (width >= 1.0 && width < 2.0) {
t = 0.05f + f * 0.33f;
r = 0.768f + 0.312f * f;
} else if (width >= 2.0 && width < 3.0) {
t = 0.38f + f * 0.58f;
r = 1.08f;
} else if (width >= 3.0 && width < 4.0) {
t = 0.96f + f * 0.48f;
r = 1.08f;
} else if (width >= 4.0 && width < 5.0) {
t = 1.44f + f * 0.46f;
r = 1.08f;
} else if (width >= 5.0 && width < 6.0) {
t = 1.9f + f * 0.6f;
r = 1.08f;
} else if (width >= 6.0) {
float ff = width - 6.0f;
t = 2.5f + ff * 0.50f;
r = 1.08f;
}
// determine angle of the line to horizontal
// core thinkness of a line
float tx = 0;
float ty = 0;
// fading edge of a line
float rx = 0;
float ry = 0;
// cap of a line
float cx = 0;
float cy = 0;
float epsilon = 0.01f;
float dx = x2 - x1;
float dy = y2 - y1;
if (Math.abs(dx) < epsilon) {
// vertical
tx = t;
ty = 0;
rx = r;
ry = 0;
if (width > 0.0 && width < 1.0) {
tx *= 8;
} else if (width == 1.0) {
tx *= 10;
}
} else if (Math.abs(dy) < epsilon) {
// horizontal
tx = 0;
ty = t;
rx = 0;
ry = r;
if (width > 0.0 && width < 1.0) {
ty *= 8;
} else if (width == 1.0) {
ty *= 10;
}
} else {
if (width < 3) {
// approximate to make things even faster
float m = dy / dx;
// and calculate tx,ty,rx,ry
if (m > -0.4142 && m <= 0.4142) {
// -22.5< angle <= 22.5, approximate to 0 (degree)
tx = t * 0.1f;
ty = t;
rx = r * 0.6f;
ry = r;
} else if (m > 0.4142 && m <= 2.4142) {
// 22.5< angle <= 67.5, approximate to 45 (degree)
tx = t * -0.7071f;
ty = t * 0.7071f;
rx = r * -0.7071f;
ry = r * 0.7071f;
} else if (m > 2.4142 || m <= -2.4142) {
// 67.5 < angle <=112.5, approximate to 90 (degree)
tx = t;
ty = t * 0.1f;
rx = r;
ry = r * 0.6f;
} else if (m > -2.4142 && m < -0.4142) {
// 112.5 < angle < 157.5, approximate to 135 (degree)
tx = t * 0.7071f;
ty = t * 0.7071f;
rx = r * 0.7071f;
ry = r * 0.7071f;
}
} else {
// calculate to exact
dx = y1 - y2;
dy = x2 - x1;
float len = (float) Math.sqrt((double) dx * dx + (double) dy * dy);
dx /= len;
dy /= len;
cx = -0.6f * dy;
cy = 0.6f * dx;
tx = t * dx;
ty = t * dy;
rx = r * dx;
ry = r * dy;
}
}
lineMeshData.reallocate(0, 0);
lineMeshData.indices.rewind();
lineMeshData.position.rewind();
lineMeshData.color0.rewind();
Vector3f v1 = new Vector3f();
Vector4f v2 = new Vector4f();
lineMeshData.position.put(v1.set(x1 - tx - rx, y1 - ty - ry, 0.0f));
lineMeshData.position.put(v1.set(x2 - tx - rx, y2 - ty - ry, 0.0f));
lineMeshData.position.put(v1.set(x1 - tx, y1 - ty, 0.0f));
lineMeshData.position.put(v1.set(x2 - tx, y2 - ty, 0.0f));
lineMeshData.position.put(v1.set(x1 + tx, y1 + ty, 0.0f));
lineMeshData.position.put(v1.set(x2 + tx, y2 + ty, 0.0f));
if (!((Math.abs(dx) < epsilon || Math.abs(dy) < epsilon) && width <= 1.0)) {
lineMeshData.position.put(v1.set(x1 + tx + rx, y1 + ty + ry, 0.0f));
lineMeshData.position.put(v1.set(x2 + tx + rx, y2 + ty + ry, 0.0f));
}
Color c = new Color();
if (!alphaBlend) {
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
} else {
lineMeshData.color0.put(c.set(v2.set(bRed, bGreen, bBlue, 0.0f)));
lineMeshData.color0.put(c.set(v2.set(bRed, bGreen, bBlue, 0.0f)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0.0f)));
lineMeshData.color0.put(c.set(v2.set(bRed, bGreen, bBlue, 0.0f)));
}
lineMesh.reload(lineMeshData);
lineMesh.render();
// cap (do not draw if too thin)
if (width >= 3) {
lineMeshData.reallocate(0, 0);
lineMeshData.indices.rewind();
lineMeshData.position.rewind();
lineMeshData.color0.rewind();
lineMeshData.position.put(v1.set(x1 - rx + cx, y1 - ry + cy, 0.0f));
lineMeshData.position.put(v1.set(x1 + rx + cx, y1 + ry + cy, 0.0f));
lineMeshData.position.put(v1.set(x1 - tx - rx, y1 - ty - ry, 0.0f));
lineMeshData.position.put(v1.set(x1 + tx + rx, y1 + ty + ry, 0.0f));
lineMeshData.position.put(v1.set(x2 - rx - cx, y2 - ry - cy, 0.0f));
lineMeshData.position.put(v1.set(x2 + rx - cx, y2 + ry - cy, 0.0f));
lineMeshData.position.put(v1.set(x2 - tx - rx, y2 - ty - ry, 0.0f));
lineMeshData.position.put(v1.set(x2 + tx + rx, y2 + ty + ry, 0.0f));
if (!alphaBlend) {
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
} else {
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
}
lineMesh.reload(lineMeshData);
lineMesh.render();
}
GL11.glEnable(GL11.GL_CULL_FACE);
}
use of org.joml.Vector4f in project Terasology by MovingBlocks.
the class TessellatorHelper method addBlockMesh.
public static void addBlockMesh(Tessellator tessellator, Vector4f color, Vector2f[] texOffsets, Vector2f[] texSizes, float size, float light1, float light2, float posX, float posY, float posZ) {
final float sizeHalf = size / 2;
tessellator.resetParams();
tessellator.setColor(new Vector4f(light1 * color.x, light1 * color.y, light1 * color.z, color.w));
tessellator.setNormal(new Vector3f(0, 1, 0));
tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[0].x, texOffsets[0].y), new Vector2f(texOffsets[0].x + texSizes[0].x, texOffsets[0].y), new Vector2f(texOffsets[0].x + texSizes[0].x, texOffsets[0].y + texSizes[0].y), new Vector2f(texOffsets[0].x, texOffsets[0].y + texSizes[0].y) });
tessellator.setNormal(new Vector3f(-1, 0, 0));
tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[1].x, texOffsets[1].y + texSizes[1].y), new Vector2f(texOffsets[1].x + texSizes[1].x, texOffsets[1].y + texSizes[1].y), new Vector2f(texOffsets[1].x + texSizes[1].x, texOffsets[1].y), new Vector2f(texOffsets[1].x, texOffsets[1].y) });
tessellator.setNormal(new Vector3f(1, 0, 0));
tessellator.addPoly(new Vector3f[] { new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[2].x, texOffsets[2].y), new Vector2f(texOffsets[2].x + texSizes[2].x, texOffsets[2].y), new Vector2f(texOffsets[2].x + texSizes[2].x, texOffsets[2].y + texSizes[2].y), new Vector2f(texOffsets[2].x, texOffsets[2].y + texSizes[2].y) });
tessellator.setColor(new Vector4f(light2 * color.x, light2 * color.y, light2 * color.z, color.w));
tessellator.setNormal(new Vector3f(0, 0, -1));
tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[3].x, texOffsets[3].y), new Vector2f(texOffsets[3].x + texSizes[3].x, texOffsets[3].y), new Vector2f(texOffsets[3].x + texSizes[3].x, texOffsets[3].y + texSizes[3].y), new Vector2f(texOffsets[3].x, texOffsets[3].y + texSizes[3].y) });
tessellator.setNormal(new Vector3f(0, 0, 1));
tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[4].x, texOffsets[4].y + texSizes[4].y), new Vector2f(texOffsets[4].x + texSizes[4].x, texOffsets[4].y + texSizes[4].y), new Vector2f(texOffsets[4].x + texSizes[4].x, texOffsets[4].y), new Vector2f(texOffsets[4].x, texOffsets[4].y) });
tessellator.setNormal(new Vector3f(0, -1, 0));
tessellator.addPoly(new Vector3f[] { new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ), new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ), new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ) }, new Vector2f[] { new Vector2f(texOffsets[5].x, texOffsets[5].y), new Vector2f(texOffsets[5].x + texSizes[5].x, texOffsets[5].y), new Vector2f(texOffsets[5].x + texSizes[5].x, texOffsets[5].y + texSizes[5].y), new Vector2f(texOffsets[5].x, texOffsets[5].y + texSizes[5].y) });
}
Aggregations