Permalink
Browse files

Implemented support for unlooped animations, and an AnimEndEvent.

Moved rendering components and systems into the rendering package.
Added a NOTICE file.
  • Loading branch information...
immortius committed Sep 21, 2012
1 parent b5a4dac commit fc7ee307dedad8fd8fcd03614ac30a92166f5c21
View
5 NOTICE
@@ -0,0 +1,5 @@
+ Terasology
+ Copyright 2011-2012 MovingBlocks
+
+ This product includes software developed by
+ MovingBlocks (http://www.movingblocks.net/).
@@ -23,7 +23,7 @@
import org.terasology.rendering.gui.widgets.UIImage;
import org.terasology.world.block.BlockComponent;
import org.terasology.world.block.BlockItemComponent;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entityFactory.DroppedBlockFactory;
import org.terasology.entitySystem.EntityManager;
@@ -18,7 +18,7 @@
import javax.vecmath.Vector3f;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.utility.LifespanComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entitySystem.EntityManager;
@@ -17,7 +17,7 @@
import javax.vecmath.Vector3f;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entitySystem.EntityManager;
import org.terasology.entitySystem.EntityRef;
@@ -17,6 +17,7 @@
import java.util.Map;
+import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import org.terasology.entitySystem.metadata.ComponentLibrary;
@@ -71,6 +72,8 @@
*/
EntityRef create(Prefab prefab, Vector3f position);
+ EntityRef create(Prefab prefab, Vector3f position, Quat4f rotation);
+
/**
* @param other
* @return A new entity with a copy of each of the other entity's components
@@ -29,6 +29,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import org.terasology.components.world.LocationComponent;
@@ -132,6 +133,22 @@ public EntityRef create(String prefabName, Vector3f position) {
return create();
}
+ @Override
+ public EntityRef create(Prefab prefab, Vector3f position, Quat4f rotation) {
+ List<Component> components = Lists.newArrayList();
+ for (Component component : prefab.listComponents()) {
+ Component newComp = componentLibrary.copy(component);
+ components.add(newComp);
+ if (newComp instanceof LocationComponent) {
+ LocationComponent loc = (LocationComponent) newComp;
+ loc.setWorldPosition(position);
+ loc.setWorldRotation(rotation);
+ }
+ }
+ components.add(new EntityInfoComponent(prefab.getName()));
+ return create(components);
+ }
+
@Override
public EntityRef create(Prefab prefab, Vector3f position) {
List<Component> components = Lists.newArrayList();
@@ -22,8 +22,10 @@
import java.util.Iterator;
import java.util.List;
+import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
+import com.bulletphysics.linearmath.QuaternionUtil;
import org.lwjgl.input.Keyboard;
import org.terasology.asset.Asset;
import org.terasology.asset.AssetManager;
@@ -33,7 +35,7 @@
import org.terasology.components.ItemComponent;
import org.terasology.components.PlayerComponent;
import org.terasology.components.SimpleAIComponent;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entityFactory.BlockItemFactory;
import org.terasology.entitySystem.EntityManager;
@@ -384,13 +386,14 @@ public void AZERTY() {
public void spawnPrefab(String prefabName) {
Camera camera = CoreRegistry.get(WorldRenderer.class).getActiveCamera();
Vector3f spawnPos = camera.getPosition();
- Vector3f offset = camera.getViewingDirection();
- offset.scale(3);
+ Vector3f offset = new Vector3f(camera.getViewingDirection());
+ offset.scale(2);
spawnPos.add(offset);
+ Quat4f rotation = QuaternionUtil.shortestArcQuat(new Vector3f(0,0,1), camera.getViewingDirection() , new Quat4f());
Prefab prefab = CoreRegistry.get(PrefabManager.class).getPrefab(prefabName);
if (prefab != null && prefab.getComponent(LocationComponent.class) != null) {
- CoreRegistry.get(EntityManager.class).create(prefab, spawnPos);
+ CoreRegistry.get(EntityManager.class).create(prefab, spawnPos, rotation);
}
}
@@ -32,7 +32,7 @@
import org.terasology.components.ItemComponent;
import org.terasology.components.PlayerComponent;
import org.terasology.components.SimpleAIComponent;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entityFactory.BlockItemFactory;
import org.terasology.entitySystem.EntityManager;
@@ -17,7 +17,7 @@
import javax.vecmath.Vector3f;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entitySystem.EntityManager;
import org.terasology.entitySystem.EntityRef;
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2012 Benjamin Glatzel <benjamin.glatzel@me.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.terasology.rendering.logic;
+
+import org.terasology.entitySystem.AbstractEvent;
+import org.terasology.rendering.assets.animation.MeshAnimation;
+
+/**
+ * @author Immortius
+ */
+public class AnimEndEvent extends AbstractEvent {
+ private MeshAnimation animation;
+
+ public AnimEndEvent(MeshAnimation animation) {
+ this.animation = animation;
+ }
+
+ public MeshAnimation getAnimation() {
+ return animation;
+ }
+}
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.terasology.components.rendering;
+package org.terasology.rendering.logic;
import javax.vecmath.Color4f;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.terasology.componentSystem.rendering;
+package org.terasology.rendering.logic;
import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY;
import static org.lwjgl.opengl.GL11.GL_NORMAL_ARRAY;
@@ -55,7 +55,7 @@
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.terasology.componentSystem.RenderSystem;
-import org.terasology.components.rendering.MeshComponent;
+import org.terasology.rendering.logic.MeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entitySystem.EntityManager;
import org.terasology.entitySystem.EntityRef;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.terasology.components.rendering;
+package org.terasology.rendering.logic;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.EntityRef;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.terasology.componentSystem.rendering;
+package org.terasology.rendering.logic;
import com.bulletphysics.linearmath.QuaternionUtil;
import com.bulletphysics.linearmath.Transform;
@@ -24,7 +24,8 @@
import org.lwjgl.opengl.GL11;
import org.terasology.componentSystem.RenderSystem;
import org.terasology.componentSystem.UpdateSubscriberSystem;
-import org.terasology.components.rendering.SkeletalMeshComponent;
+import org.terasology.rendering.assets.animation.MeshAnimation;
+import org.terasology.rendering.logic.SkeletalMeshComponent;
import org.terasology.components.world.LocationComponent;
import org.terasology.entitySystem.EntityManager;
import org.terasology.entitySystem.EntityRef;
@@ -34,7 +35,6 @@
import org.terasology.entitySystem.event.AddComponentEvent;
import org.terasology.game.CoreRegistry;
import org.terasology.logic.LocalPlayer;
-import org.terasology.logic.manager.ShaderManager;
import org.terasology.rendering.assets.animation.MeshAnimationFrame;
import org.terasology.rendering.assets.skeletalmesh.Bone;
import org.terasology.rendering.world.WorldRenderer;
@@ -47,11 +47,7 @@
import java.util.logging.Logger;
import static org.lwjgl.opengl.GL11.glBegin;
-import static org.lwjgl.opengl.GL11.glColor4f;
-import static org.lwjgl.opengl.GL11.glDisable;
-import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
-import static org.lwjgl.opengl.GL11.glLineWidth;
import static org.lwjgl.opengl.GL11.glMultMatrix;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
@@ -109,44 +105,62 @@ public void newSkeleton(AddComponentEvent event, EntityRef entity) {
@Override
public void update(float delta) {
- Vector3f newPos = new Vector3f();
- Quat4f newRot = new Quat4f();
-
for (EntityRef entity : entityManager.iteratorEntities(SkeletalMeshComponent.class, LocationComponent.class)) {
SkeletalMeshComponent skeletalMeshComp = entity.getComponent(SkeletalMeshComponent.class);
- LocationComponent loc = entity.getComponent(LocationComponent.class);
if (skeletalMeshComp.animation != null && skeletalMeshComp.animation.getFrameCount() > 0) {
skeletalMeshComp.animationTime += delta * skeletalMeshComp.animationRate;
float framePos = skeletalMeshComp.animationTime / skeletalMeshComp.animation.getTimePerFrame();
- while ((int) framePos >= skeletalMeshComp.animation.getFrameCount()) {
- framePos -= skeletalMeshComp.animation.getFrameCount();
- }
- int frameId = (int) framePos;
- MeshAnimationFrame frameA = skeletalMeshComp.animation.getFrame(frameId);
- MeshAnimationFrame frameB = skeletalMeshComp.animation.getFrame((frameId + 1) % skeletalMeshComp.animation.getFrameCount());
- float interpolationVal = framePos - frameId;
-
- for (int i = 0; i < skeletalMeshComp.animation.getBoneCount(); ++i) {
- EntityRef boneEntity = skeletalMeshComp.boneEntities.get(skeletalMeshComp.animation.getBoneName(i));
- if (boneEntity == null) {
- continue;
+
+ if (skeletalMeshComp.loop) {
+ while ((int) framePos >= skeletalMeshComp.animation.getFrameCount()) {
+ framePos -= skeletalMeshComp.animation.getFrameCount();
+ skeletalMeshComp.animationTime -= skeletalMeshComp.animation.getTimePerFrame() * skeletalMeshComp.animation.getFrameCount();
}
- LocationComponent boneLoc = boneEntity.getComponent(LocationComponent.class);
- if (boneLoc != null) {
-
- newPos.interpolate(frameA.getPosition(i), frameB.getPosition(i), interpolationVal);
- boneLoc.setLocalPosition(newPos);
- newRot.interpolate(frameA.getRotation(i), frameB.getRotation(i), interpolationVal);
- newRot.normalize();
- boneLoc.setLocalRotation(newRot);
- boneEntity.saveComponent(boneLoc);
+ int frameId = (int) framePos;
+ MeshAnimationFrame frameA = skeletalMeshComp.animation.getFrame(frameId);
+ MeshAnimationFrame frameB = skeletalMeshComp.animation.getFrame((frameId + 1) % skeletalMeshComp.animation.getFrameCount());
+ updateSkeleton(skeletalMeshComp, frameA, frameB, framePos - frameId);
+ } else {
+ if ((int) framePos >= skeletalMeshComp.animation.getFrameCount()) {
+ updateSkeleton(skeletalMeshComp, skeletalMeshComp.animation.getFrame(skeletalMeshComp.animation.getFrameCount() - 1), skeletalMeshComp.animation.getFrame(skeletalMeshComp.animation.getFrameCount() - 1), 1.0f);
+ MeshAnimation animation = skeletalMeshComp.animation;
+ skeletalMeshComp.animationTime = 0;
+ skeletalMeshComp.animation = null;
+ entity.send(new AnimEndEvent(animation));
+ } else {
+ int frameId = (int) framePos;
+ MeshAnimationFrame frameA = skeletalMeshComp.animation.getFrame(frameId);
+ MeshAnimationFrame frameB = (frameId + 1 >= skeletalMeshComp.animation.getFrameCount()) ? frameA : skeletalMeshComp.animation.getFrame(frameId + 1);
+ updateSkeleton(skeletalMeshComp, frameA, frameB, framePos - frameId);
}
}
entity.saveComponent(skeletalMeshComp);
}
}
}
+ private void updateSkeleton(SkeletalMeshComponent skeletalMeshComp, MeshAnimationFrame frameA, MeshAnimationFrame frameB, float interpolationVal) {
+ Vector3f newPos = new Vector3f();
+ Quat4f newRot = new Quat4f();
+
+ for (int i = 0; i < skeletalMeshComp.animation.getBoneCount(); ++i) {
+ EntityRef boneEntity = skeletalMeshComp.boneEntities.get(skeletalMeshComp.animation.getBoneName(i));
+ if (boneEntity == null) {
+ continue;
+ }
+ LocationComponent boneLoc = boneEntity.getComponent(LocationComponent.class);
+ if (boneLoc != null) {
+
+ newPos.interpolate(frameA.getPosition(i), frameB.getPosition(i), interpolationVal);
+ boneLoc.setLocalPosition(newPos);
+ newRot.interpolate(frameA.getRotation(i), frameB.getRotation(i), interpolationVal);
+ newRot.normalize();
+ boneLoc.setLocalRotation(newRot);
+ boneEntity.saveComponent(boneLoc);
+ }
+ }
+ }
+
@Override
public void renderOpaque() {
boolean carryingTorch = CoreRegistry.get(LocalPlayer.class).isCarryingTorch();
@@ -59,7 +59,7 @@
import org.lwjgl.opengl.GL11;
import org.terasology.componentSystem.RenderSystem;
import org.terasology.componentSystem.controllers.LocalPlayerSystem;
-import org.terasology.componentSystem.rendering.MeshRenderer;
+import org.terasology.rendering.logic.MeshRenderer;
import org.terasology.components.PlayerComponent;
import org.terasology.entitySystem.EntityManager;
import org.terasology.game.ComponentSystemManager;

0 comments on commit fc7ee30

Please sign in to comment.