use of org.joml.Vector3f in project Terasology by MovingBlocks.
the class IconMeshFactory method addPixel.
private static void addPixel(StandardMeshData mesh, Vector2fc position, float size, Color c) {
Vector3f pos = new Vector3f();
Vector3f norm = new Vector3f();
final float sizeHalf = size / 2;
int firstIndex = mesh.position.getPosition();
// top
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, sizeHalf, -sizeHalf));
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.set(position, 0.0f).add(-sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, sizeHalf, -sizeHalf));
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.set(position, 0.0f).add(sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, -sizeHalf, -sizeHalf));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(1.0f, 0, 0));
mesh.color0.put(c);
}
// darkern for sides facing left, right and bottom
Color cd = new Color(c.rf() * 0.6f, c.gf() * 0.6f, c.bf() * 0.6f, c.af());
// back
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, -sizeHalf, -sizeHalf));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, 0, -1.0f));
mesh.color0.put(cd);
}
// front
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, sizeHalf, sizeHalf));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, 0, 1.0f));
mesh.color0.put(cd);
}
// bottom
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, -sizeHalf, -sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(sizeHalf, -sizeHalf, sizeHalf));
mesh.position.put(pos.set(position, 0.0f).add(-sizeHalf, -sizeHalf, sizeHalf));
for (int i = 0; i < 4; i++) {
mesh.normal.put(norm.set(0, -1, 0f));
mesh.color0.put(cd);
}
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.Vector3f in project Terasology by MovingBlocks.
the class BoundingBoxRenderer method renderOverlay.
@Override
public void renderOverlay() {
if (config.getRendering().getDebug().isRenderEntityBoundingBoxes()) {
GL33.glDepthFunc(GL33.GL_ALWAYS);
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Vector3f worldPos = new Vector3f();
Vector3f worldPositionCameraSpace = new Vector3f();
worldPos.sub(cameraPosition, worldPositionCameraSpace);
Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
int index = 0;
meshData.reallocate(0, 0);
meshData.indices.rewind();
meshData.position.rewind();
meshData.color0.rewind();
Vector3f worldPosition = new Vector3f();
Quaternionf worldRot = new Quaternionf();
Matrix4f transform = new Matrix4f();
AABBf bounds = new AABBf(0, 0, 0, 0, 0, 0);
for (EntityRef entity : entityManager.getEntitiesWith(LocationComponent.class)) {
LocationComponent location = entity.getComponent(LocationComponent.class);
location.getWorldPosition(worldPosition);
location.getWorldRotation(worldRot);
BoxShapeComponent boxShapeComponent = entity.getComponent(BoxShapeComponent.class);
if (boxShapeComponent != null) {
bounds.set(0, 0, 0, 0, 0, 0);
bounds.expand(new Vector3f(boxShapeComponent.extents).div(2.0f));
transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
bounds.transform(transform);
index = addRenderBound(meshData, bounds, index);
}
CapsuleShapeComponent capsuleComponent = entity.getComponent(CapsuleShapeComponent.class);
if (capsuleComponent != null) {
bounds.set(0, 0, 0, 0, 0, 0);
bounds.expand(new Vector3f(capsuleComponent.radius, capsuleComponent.height / 2.0f, capsuleComponent.radius).div(2.0f));
transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
bounds.transform(transform);
index = addRenderBound(meshData, bounds, index);
}
CylinderShapeComponent cylinderShapeComponent = entity.getComponent(CylinderShapeComponent.class);
if (cylinderShapeComponent != null) {
bounds.set(0, 0, 0, 0, 0, 0);
bounds.expand(new Vector3f(cylinderShapeComponent.radius, cylinderShapeComponent.height / 2.0f, cylinderShapeComponent.radius).div(2.0f));
transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
bounds.transform(transform);
index = addRenderBound(meshData, bounds, index);
}
SphereShapeComponent sphereShapeComponent = entity.getComponent(SphereShapeComponent.class);
if (sphereShapeComponent != null) {
bounds.set(0, 0, 0, 0, 0, 0);
bounds.expand(new Vector3f(sphereShapeComponent.radius).div(2.0f));
transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
bounds.transform(transform);
index = addRenderBound(meshData, bounds, index);
}
}
material.enable();
mesh.reload(meshData);
mesh.render();
GL33.glDepthFunc(GL33.GL_LEQUAL);
}
}
use of org.joml.Vector3f in project Terasology by MovingBlocks.
the class GLTFAttributeMapping method readVec3FBuffer.
public static void readVec3FBuffer(byte[] buffer, GLTFAccessor accessor, GLTFBufferView bufferView, VertexAttributeBinding<Vector3fc, Vector3f> 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();
}
Vector3f pos = new Vector3f();
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.Vector3f in project Terasology by MovingBlocks.
the class MeshRenderer method renderEntitiesByMaterial.
private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quaternionf worldRot = new Quaternionf();
Vector3f worldPos = new Vector3f();
Matrix3f normalMatrix = new Matrix3f();
Matrix4f matrixCameraSpace = new Matrix4f();
Matrix4f modelViewMatrix = new Matrix4f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (Material material : meshByMaterial.keySet()) {
if (material.isRenderable()) {
material.enable();
material.setFloat("sunlight", 1.0f, true);
material.setFloat("blockLight", 1.0f, true);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.bindTextures();
Set<EntityRef> entities = meshByMaterial.get(material);
lastRendered = entities.size();
for (EntityRef entity : entities) {
MeshComponent meshComp = entity.getComponent(MeshComponent.class);
LocationComponent location = entity.getComponent(LocationComponent.class);
if (isHidden(entity, meshComp) || location == null || meshComp.mesh == null) {
continue;
}
Vector3f worldPosition = location.getWorldPosition(new Vector3f());
if (!worldPosition.isFinite() && !isRelevant(entity, worldPosition)) {
continue;
}
if (meshComp.mesh.isDisposed()) {
logger.error("Attempted to render disposed mesh");
continue;
}
worldRot.set(location.getWorldRotation(new Quaternionf()));
worldPos.set(location.getWorldPosition(new Vector3f()));
float worldScale = location.getWorldScale();
Vector3f offsetFromCamera = worldPos.sub(cameraPosition, new Vector3f());
matrixCameraSpace.translationRotateScale(offsetFromCamera, worldRot, worldScale);
AABBf aabb = meshComp.mesh.getAABB().transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
modelViewMatrix.set(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
modelViewMatrix.get(tempMatrixBuffer44);
modelViewMatrix.normal(normalMatrix).get(tempMatrixBuffer33);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
material.setFloat3("colorOffset", meshComp.color.rf(), meshComp.color.gf(), meshComp.color.bf(), true);
material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
material.setFloat("blockLight", Math.max(worldRenderer.getBlockLightIntensityAt(worldPos), meshComp.selfLuminance), true);
meshComp.mesh.render();
}
}
}
}
}
use of org.joml.Vector3f in project Terasology by MovingBlocks.
the class BlockCommands method replaceBlock.
@Command(shortDescription = "Replaces a block in front of user", helpText = "Replaces a block in front of the user at the specified max distance", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public void replaceBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "maxDistance", required = false) Integer maxDistanceParam) {
int maxDistance = maxDistanceParam != null ? maxDistanceParam : 12;
EntityRef playerEntity = sender.getComponent(ClientComponent.class).character;
EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(playerEntity);
LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
targetSystem.updateTarget(gazeLocation.getWorldPosition(new Vector3f()), gazeLocation.getWorldDirection(new Vector3f()), maxDistance);
EntityRef target = targetSystem.getTarget();
BlockComponent targetLocation = target.getComponent(BlockComponent.class);
if (matchingUris.size() == 1) {
Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
if (def.isPresent()) {
BlockFamily blockFamily = blockManager.getBlockFamily(uri);
Block block = blockManager.getBlock(blockFamily.getURI());
world.setBlock(targetLocation.getPosition(), block);
} else if (matchingUris.size() > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Non-unique shape name, possible matches: ");
Iterator<ResourceUrn> shapeUris = sortItems(matchingUris).iterator();
while (shapeUris.hasNext()) {
builder.append(shapeUris.next().toString());
if (shapeUris.hasNext()) {
builder.append(", ");
}
}
}
}
}
Aggregations