use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class MeshRenderer method renderEntitiesByMaterial.
private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quat4f worldRot = new Quat4f();
Vector3f worldPos = new Vector3f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (Material material : meshByMaterial.keySet()) {
if (material.isRenderable()) {
OpenGLMesh lastMesh = null;
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 || !isRelevant(entity, location.getWorldPosition())) {
continue;
}
if (meshComp.mesh.isDisposed()) {
logger.error("Attempted to render disposed mesh");
continue;
}
location.getWorldRotation(worldRot);
location.getWorldPosition(worldPos);
float worldScale = location.getWorldScale();
Transform toWorldSpace = new Transform(worldPos, worldRot, worldScale);
Vector3f offsetFromCamera = new Vector3f();
offsetFromCamera.sub(worldPos, cameraPosition);
Matrix4f matrixCameraSpace = new Matrix4f(worldRot, offsetFromCamera, worldScale);
AABB aabb = meshComp.mesh.getAABB().transform(toWorldSpace);
if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
if (meshComp.mesh != lastMesh) {
if (lastMesh != null) {
lastMesh.postRender();
}
lastMesh = (OpenGLMesh) meshComp.mesh;
lastMesh.preRender();
}
Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.setMatrix4("worldViewMatrix", 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);
lastMesh.doRender();
}
}
if (lastMesh != null) {
lastMesh.postRender();
}
}
}
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class SkeletonRenderer method renderOverlay.
@Override
public void renderOverlay() {
if (config.getRendering().getDebug().isRenderSkeletons()) {
glDisable(GL_DEPTH_TEST);
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Material material = Assets.getMaterial("engine:white").get();
material.setFloat("sunlight", 1.0f, true);
material.setFloat("blockLight", 1.0f, true);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
glLineWidth(2);
Vector3f worldPos = new Vector3f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
LocationComponent location = entity.getComponent(LocationComponent.class);
location.getWorldPosition(worldPos);
Vector3f worldPositionCameraSpace = new Vector3f();
worldPositionCameraSpace.sub(worldPos, cameraPosition);
float worldScale = location.getWorldScale();
Matrix4f matrixCameraSpace = new Matrix4f(new Quat4f(0, 0, 0, 1), worldPositionCameraSpace, worldScale);
Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true);
MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
renderBone(skeletalMesh.rootBone, worldPos);
}
glEnable(GL_DEPTH_TEST);
}
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class MD5ParserCommon method correctQuat4f.
public static Quat4f correctQuat4f(Quat4f rot) {
Quat4f result = new Quat4f(CORRECTION_QUATERNION);
result.mul(rot);
return result;
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class CameraClientSystem method mountCamera.
private void mountCamera() {
ClientComponent clientComponent = localPlayer.getClientEntity().getComponent(ClientComponent.class);
EntityRef targetEntityForCamera = GazeAuthoritySystem.getGazeEntityForCharacter(clientComponent.character);
LocationComponent cameraLocation = clientComponent.camera.getComponent(LocationComponent.class);
// if the camera already has a location, use that as the relative position of the camera
if (cameraLocation != null) {
Location.attachChild(targetEntityForCamera, clientComponent.camera, cameraLocation.getLocalPosition(), new Quat4f(Quat4f.IDENTITY));
} else {
Location.attachChild(targetEntityForCamera, clientComponent.camera, Vector3f.zero(), new Quat4f(Quat4f.IDENTITY));
}
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class FirstPersonClientSystem method linkHeldItemLocationForLocalPlayer.
/**
* Changes held item entity.
*
* <p>Detaches old held item and removes it's components. Adds components to new held item and
* attaches it to the mount point entity.</p>
*/
private void linkHeldItemLocationForLocalPlayer(EntityRef newItem) {
if (!newItem.equals(currentHeldItem)) {
EntityRef camera = localPlayer.getCameraEntity();
FirstPersonHeldItemMountPointComponent mountPointComponent = camera.getComponent(FirstPersonHeldItemMountPointComponent.class);
if (mountPointComponent != null) {
// currentHeldItem is at this point the old item
if (currentHeldItem != EntityRef.NULL) {
currentHeldItem.destroy();
}
// use the hand if there is no new item
EntityRef newHeldItem;
if (newItem == EntityRef.NULL) {
newHeldItem = getHandEntity();
} else {
newHeldItem = newItem;
}
// create client side held item entity
currentHeldItem = entityManager.create();
// add the visually relevant components
for (Component component : newHeldItem.iterateComponents()) {
if (component instanceof VisualComponent) {
currentHeldItem.addComponent(component);
}
}
// ensure world location is set
currentHeldItem.addComponent(new LocationComponent());
currentHeldItem.addComponent(new ItemIsHeldComponent());
FirstPersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(FirstPersonHeldItemTransformComponent.class);
if (heldItemTransformComponent == null) {
heldItemTransformComponent = new FirstPersonHeldItemTransformComponent();
currentHeldItem.addComponent(heldItemTransformComponent);
}
Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem, heldItemTransformComponent.translate, new Quat4f(TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.y, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.x, TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.z), heldItemTransformComponent.scale);
}
}
}
Aggregations