Skip to content

Commit

Permalink
Integrated nearest rendering mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
immortius committed Aug 17, 2013
1 parent 9c55f31 commit cceb5d4
Show file tree
Hide file tree
Showing 15 changed files with 761 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/terasology/asset/AssetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.terasology.audio.loaders.OggSoundLoader;
import org.terasology.audio.loaders.OggStreamingSoundLoader;
import org.terasology.entitySystem.prefab.PrefabLoader;
import org.terasology.rendering.assetLoaders.md5.MD5AnimationLoader;
import org.terasology.rendering.assetLoaders.md5.MD5SkeletonLoader;
import org.terasology.rendering.md5.MD5AnimationLoader;
import org.terasology.rendering.md5.MD5SkeletonLoader;
import org.terasology.rendering.assets.font.FontLoader;
import org.terasology.rendering.assets.material.MaterialLoader;
import org.terasology.rendering.assets.mesh.ObjMeshLoader;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/terasology/config/RenderingConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class RenderingConfig {
private boolean dynamicShadowsPcfFiltering = false;
private boolean volumetricFog = false;
private boolean cloudShadows = false;
private boolean renderNearest = true;
private int particleEffectLimit = 10;
private int meshLimit = 20;

private RenderingDebugConfig debug = new RenderingDebugConfig();

Expand Down Expand Up @@ -328,6 +331,30 @@ public void setCloudShadows(boolean cloudShadows) {
this.cloudShadows = cloudShadows;
}

public boolean isRenderNearest() {
return renderNearest;
}

public void setRenderNearest(boolean renderNearest) {
this.renderNearest = renderNearest;
}

public int getParticleEffectLimit() {
return particleEffectLimit;
}

public void setParticleEffectLimit(int particleEffectLimit) {
this.particleEffectLimit = particleEffectLimit;
}

public int getMeshLimit() {
return meshLimit;
}

public void setMeshLimit(int meshLimit) {
this.meshLimit = meshLimit;
}

public RenderingDebugConfig getDebug() {
return debug;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
package org.terasology.engine.modes.loadProcesses;

import org.terasology.asset.AssetType;
import org.terasology.asset.AssetUri;
import org.terasology.engine.CoreRegistry;
import org.terasology.engine.modes.LoadProcess;
import org.terasology.rendering.ShaderManager;
import org.terasology.rendering.primitives.Tessellator;
import org.terasology.rendering.primitives.TessellatorHelper;

import javax.vecmath.Vector4f;

/**
* @author Immortius
Expand All @@ -31,6 +37,13 @@ public String getMessage() {
@Override
public boolean step() {
CoreRegistry.get(ShaderManager.class).initShaders();

// TODO: This should be elsewhere
// Create gelatinousCubeMesh
Tessellator tessellator = new Tessellator();
TessellatorHelper.addBlockMesh(tessellator, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f), 0.8f, 0.8f, 0.6f, 0f, 0f, 0f);
TessellatorHelper.addBlockMesh(tessellator, new Vector4f(1.0f, 1.0f, 1.0f, 0.6f), 1.0f, 1.0f, 0.8f, 0f, 0f, 0f);
tessellator.generateMesh(new AssetUri(AssetType.MESH, "engine", "gelatinousCube"));
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/terasology/logic/actions/TunnelAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public void onActivate(ActivateEvent event, EntityRef entity) {
int blockCounter = MAX_DESTROYED_BLOCKS;
for (int s = 0; s <= 512; s++) {
origin.add(dir);
if (!worldProvider.isBlockRelevant(origin)) {
break;
}

for (int i = 0; i < 64; i++) {
Vector3f direction = random.randomVector3f();
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/org/terasology/logic/location/DistanceComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2013 Moving Blocks
*
* 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.logic.location;

import org.terasology.entitySystem.EntityRef;

import javax.vecmath.Tuple3f;
import javax.vecmath.Vector3f;
import java.util.Comparator;

/**
* Comparator that compares the distances to a location of two Entities.
* Closer is smaller, hence return -1, which results in lower index for
* closer the object when sorting.
* Entities without a location component are assumed to be infinitely far
* away from the location.
* The location can be given in the constructor and set to a different value
* afterwards.
*/
public class DistanceComparator implements Comparator<EntityRef> {
/**
* The distance to this point is taken for the comparison of
* distances.
*/
private final Vector3f origin;
/**
* Used to store the location of Entities temporarily. Having
* this vector pre-allocated saves a lot of memory allocations for new
* vectors.
*/
private final Vector3f temp = new Vector3f();

/**
* The default constructor will set the location to calculate the
* distances from to {0, 0, 0}.
*/
/**
* The default constructor will set the location to calculate the
* distances from to {0, 0, 0}.
*/
public DistanceComparator() {
origin = new Vector3f();
}

/**
* Creates this Distance comparator and sets the temp to the
* given parameter.
* The temp is used to calculate distances from.
* @param temp used to calculate distances from when comparing entities.
*/
/**
* Creates this Distance comparator and sets the temp to the
* given parameter.
* The temp is used to calculate distances from.
*
* @param origin used to calculate distances from when comparing entities.
*/
public DistanceComparator(Vector3f origin) {
this.origin = new Vector3f(origin);
}

@Override
public int compare(EntityRef o1, EntityRef o2) {
LocationComponent loc1 = o1.getComponent(LocationComponent.class);
LocationComponent loc2 = o2.getComponent(LocationComponent.class);
if (loc1 == null && loc2 == null) {
return 0;
} else if (loc1 == null) {
return 1;
} else if (loc2 == null) {
return -1;
}
loc1.getWorldPosition(temp);
temp.sub(origin);
float dis1 = temp.lengthSquared();
loc2.getWorldPosition(temp);
temp.sub(origin);
float dis2 = temp.lengthSquared();
if (dis1 < dis2) {
return -1;
} else if (dis2 < dis1) {
return 1;
} else {
//dis1 == dis2
return 0;
}
}

/**
* Sets the origin, which is used to calculate the distance from.
* This method should not be called while sorting. If done anyway, the
* contract of compare method will be broken and the sorting results
* are undefined, if by chance no Exception is thrown.
*
* @param newOrigin the new location to calculate distances from.
*/
public void setOrigin(Tuple3f newOrigin) {
origin.set(newOrigin);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public float getLocalScale() {
return scale;
}

/**
* @return A new vector containing the world location.
*/
public Vector3f getWorldPosition() {
return getWorldPosition(new Vector3f());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.terasology.asset.Assets;
import org.terasology.config.Config;
import org.terasology.entitySystem.EntityManager;
import org.terasology.entitySystem.EntityRef;
import org.terasology.entitySystem.RegisterMode;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.lifecycleEvents.BeforeDeactivateComponent;
import org.terasology.entitySystem.lifecycleEvents.OnActivatedComponent;
import org.terasology.entitySystem.systems.In;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.entitySystem.systems.RenderSystem;
Expand All @@ -30,6 +34,7 @@
import org.terasology.logic.particles.BlockParticleEffectComponent.Particle;
import org.terasology.rendering.assets.material.Material;
import org.terasology.rendering.assets.texture.Texture;
import org.terasology.rendering.logic.NearestSortingList;
import org.terasology.rendering.world.WorldRenderer;
import org.terasology.utilities.procedural.FastRandom;
import org.terasology.world.WorldProvider;
Expand All @@ -42,6 +47,7 @@
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;
import java.nio.FloatBuffer;
import java.util.Arrays;
import java.util.Iterator;

import static org.lwjgl.opengl.GL11.GL_ONE;
Expand Down Expand Up @@ -90,7 +96,11 @@ public class BlockParticleEmitterSystem implements UpdateSubscriberSystem, Rende
@In
private WorldRenderer worldRenderer;

@In
private Config config;

private FastRandom random = new FastRandom();
private NearestSortingList sorter = new NearestSortingList();
private int displayList = 0;

public void initialise() {
Expand All @@ -100,11 +110,13 @@ public void initialise() {
drawParticle();
glEndList();
}
sorter.initialise(worldRenderer.getActiveCamera());
}

@Override
public void shutdown() {
glDeleteLists(displayList, 1);
sorter.stop();
}

public void update(float delta) {
Expand Down Expand Up @@ -134,6 +146,17 @@ public void update(float delta) {
}
}

@ReceiveEvent(components = {BlockParticleEffectComponent.class, LocationComponent.class})
public void onActivated(OnActivatedComponent event, EntityRef entity) {
sorter.add(entity);
}

@ReceiveEvent(components = {BlockParticleEffectComponent.class, LocationComponent.class})
public void onDeactivated(BeforeDeactivateComponent event, EntityRef entity) {
sorter.remove(entity);
}


private void spawnParticle(BlockParticleEffectComponent particleEffect) {

Particle p = new Particle();
Expand Down Expand Up @@ -198,12 +221,20 @@ protected void updatePosition(Particle particle, float delta) {
}

public void renderAlphaBlend() {
if (config.getRendering().isRenderNearest()) {
render(Arrays.asList(sorter.getNearest(config.getRendering().getParticleEffectLimit())));
} else {
render(entityManager.getEntitiesWith(BlockParticleEffectComponent.class, LocationComponent.class));
}
}

private void render(Iterable<EntityRef> particleEntities) {
Assets.getMaterial("engine:particle").enable();
glDisable(GL11.GL_CULL_FACE);

Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();

for (EntityRef entity : entityManager.getEntitiesWith(BlockParticleEffectComponent.class, LocationComponent.class)) {
for (EntityRef entity : particleEntities) {
LocationComponent location = entity.getComponent(LocationComponent.class);
Vector3f worldPos = location.getWorldPosition();

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/terasology/logic/players/LocalPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ public boolean isValid() {
}

public Vector3f getPosition() {
return getPosition(new Vector3f());
}

public Vector3f getPosition(Vector3f out) {
LocationComponent location = getCharacterEntity().getComponent(LocationComponent.class);
if (location == null) {
return new Vector3f();
return out;
}
return location.getWorldPosition();
return location.getWorldPosition(out);
}

public Quat4f getRotation() {
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/org/terasology/math/TeraMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,39 @@ public static int ceilToInt(float val) {
}

// TODO: Move to a matrix util class

/**
* Copies the given matrix into a newly allocated FloatBuffer.
* The order of the elements is column major (as used by OpenGL).
* @param m the matrix to copy
* @return A new FloatBuffer containing the matrix in column-major form.
*/
public static FloatBuffer matrixToFloatBuffer(Matrix4f m) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
matrixToFloatBuffer(m, buffer);
return buffer;
}

/**
* Copies the given matrix into a newly allocated FloatBuffer.
* The order of the elements is column major (as used by OpenGL).
* @param m the matrix to copy
* @return A new FloatBuffer containing the matrix in column-major form.
*/
public static FloatBuffer matrixToFloatBuffer(Matrix3f m) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(9);
matrixToFloatBuffer(m, buffer);
return buffer;
}

public static void matrixToFloatBuffer(Matrix3f m, FloatBuffer fb) {
/**
* Copies the given matrix into an existing FloatBuffer.
* The order of the elements is column major (as used by OpenGL).
* @param m the matrix to copy
* @param fb the float buffer to copy the matrix into
* @return The provided float buffer.
*/
public static FloatBuffer matrixToFloatBuffer(Matrix3f m, FloatBuffer fb) {
fb.put(m.m00);
fb.put(m.m10);
fb.put(m.m20);
Expand All @@ -431,9 +451,17 @@ public static void matrixToFloatBuffer(Matrix3f m, FloatBuffer fb) {
fb.put(m.m22);

fb.flip();
return fb;
}

public static void matrixToFloatBuffer(Matrix4f m, FloatBuffer fb) {
/**
* Copies the given matrix into an existing FloatBuffer.
* The order of the elements is column major (as used by OpenGL).
* @param m the matrix to copy
* @param fb the float buffer to copy the matrix into
* @return The provided float buffer.
*/
public static FloatBuffer matrixToFloatBuffer(Matrix4f m, FloatBuffer fb) {
fb.put(m.m00);
fb.put(m.m10);
fb.put(m.m20);
Expand All @@ -452,6 +480,7 @@ public static void matrixToFloatBuffer(Matrix4f m, FloatBuffer fb) {
fb.put(m.m33);

fb.flip();
return fb;
}

public static Matrix4f createViewMatrix(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) {
Expand Down
Loading

0 comments on commit cceb5d4

Please sign in to comment.