Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Shapes now used to in the display of block icons and held blocks

 - Alpha testing now enabled when rendering translucent held blocks
  • Loading branch information...
commit 986cd37e857ee9d7f3fab8306ec6b2da3ae3f8f1 1 parent f9891c0
@immortius immortius authored
View
1  .idea/dictionaries/Immortius.xml
@@ -2,6 +2,7 @@
<dictionary name="Immortius">
<words>
<w>biome</w>
+ <w>tessellator</w>
</words>
</dictionary>
</component>
View
2  LOGS/.gitignore
@@ -0,0 +1,2 @@
+
+*.log*
View
10 src/org/terasology/logic/characters/GelatinousCube.java
@@ -22,7 +22,7 @@
import org.terasology.logic.manager.TextureManager;
import org.terasology.model.structures.AABB;
import org.terasology.rendering.primitives.Mesh;
-import org.terasology.rendering.primitives.MeshCollection;
+import org.terasology.rendering.primitives.TessellatorHelper;
import org.terasology.rendering.primitives.Tessellator;
import org.terasology.rendering.world.WorldRenderer;
import org.terasology.utilities.MathHelper;
@@ -57,10 +57,10 @@ public GelatinousCube(WorldRenderer parent) {
_randomSize = (float) MathHelper.clamp((_parent.getWorldProvider().getRandom().randomDouble() + 1.0) / 2.0 + 0.15);
_randomColorId = Math.abs(_parent.getWorldProvider().getRandom().randomInt()) % COLORS.length;
- MeshCollection.addBlockMesh(new Vector4f(COLORS[_randomColorId].x, COLORS[_randomColorId].y, COLORS[_randomColorId].y, 1.0f), 0.8f * _randomSize, 0.8f, 0.6f, 0f, 0f, 0f);
- MeshCollection.addBlockMesh(new Vector4f(COLORS[_randomColorId].x, COLORS[_randomColorId].y, COLORS[_randomColorId].y, 0.6f), 1.0f * _randomSize, 1.0f, 0.8f, 0f, 0f, 0f);
- _mesh = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Tessellator tessellator = new Tessellator();
+ TessellatorHelper.addBlockMesh(tessellator, new Vector4f(COLORS[_randomColorId].x, COLORS[_randomColorId].y, COLORS[_randomColorId].y, 1.0f), 0.8f * _randomSize, 0.8f, 0.6f, 0f, 0f, 0f);
+ TessellatorHelper.addBlockMesh(tessellator, new Vector4f(COLORS[_randomColorId].x, COLORS[_randomColorId].y, COLORS[_randomColorId].y, 0.6f), 1.0f * _randomSize, 1.0f, 0.8f, 0f, 0f, 0f);
+ _mesh = tessellator.generateMesh();
}
public void update() {
View
14 src/org/terasology/logic/characters/Player.java
@@ -43,7 +43,7 @@
import org.terasology.rendering.cameras.FirstPersonCamera;
import org.terasology.rendering.physics.BulletPhysicsRenderer;
import org.terasology.rendering.primitives.Mesh;
-import org.terasology.rendering.primitives.MeshCollection;
+import org.terasology.rendering.primitives.TessellatorHelper;
import org.terasology.rendering.primitives.Tessellator;
import org.terasology.rendering.world.WorldRenderer;
import org.terasology.utilities.MathHelper;
@@ -477,9 +477,9 @@ public void renderExtractionOverlay() {
Vector2f texPos = new Vector2f(0.0f, 0.0f);
Vector2f texWidth = new Vector2f(0.0624f, 0.0624f);
- MeshCollection.addBlockMesh(new Vector4f(1, 1, 1, 1), texPos, texWidth, 1.001f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
- _overlayMesh = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Tessellator tessellator = new Tessellator();
+ TessellatorHelper.addBlockMesh(tessellator, new Vector4f(1, 1, 1, 1), texPos, texWidth, 1.001f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
+ _overlayMesh = tessellator.generateMesh();
}
glMatrixMode(GL_TEXTURE);
@@ -531,9 +531,9 @@ public void renderHand() {
Vector2f texPos = new Vector2f(40.0f * 0.015625f, 32.0f * 0.03125f);
Vector2f texWidth = new Vector2f(4.0f * 0.015625f, -12.0f * 0.03125f);
- MeshCollection.addBlockMesh(new Vector4f(1, 1, 1, 1), texPos, texWidth, 1.0f, 1.0f, 0.9f, 0.0f, 0.0f, 0.0f);
- _handMesh = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Tessellator tessellator = new Tessellator();
+ TessellatorHelper.addBlockMesh(tessellator, new Vector4f(1, 1, 1, 1), texPos, texWidth, 1.0f, 1.0f, 0.9f, 0.0f, 0.0f, 0.0f);
+ _handMesh = tessellator.generateMesh();
}
_handMesh.render();
View
38 src/org/terasology/model/blocks/Block.java
@@ -23,7 +23,6 @@
import org.terasology.model.structures.AABB;
import org.terasology.rendering.interfaces.IGameObject;
import org.terasology.rendering.primitives.Mesh;
-import org.terasology.rendering.primitives.MeshCollection;
import org.terasology.rendering.primitives.Tessellator;
import javax.imageio.ImageIO;
@@ -52,6 +51,18 @@
public static final int ATLAS_ELEMENTS_PER_ROW_AND_COLUMN = ATLAS_SIZE_IN_PX / TEXTURE_SIZE_IN_PX;
public static final float TEXTURE_OFFSET = 0.0625f;
public static final float TEXTURE_OFFSET_WIDTH = 0.0624f;
+
+ private static final EnumMap<Side, Float> DIRECTION_LIT_LEVEL = new EnumMap<Side, Float>(Side.class);
+
+ static
+ {
+ DIRECTION_LIT_LEVEL.put(Side.TOP, 0.9f);
+ DIRECTION_LIT_LEVEL.put(Side.BOTTOM, 0.9f);
+ DIRECTION_LIT_LEVEL.put(Side.FRONT, 1.0f);
+ DIRECTION_LIT_LEVEL.put(Side.BACK, 1.0f);
+ DIRECTION_LIT_LEVEL.put(Side.LEFT, 0.75f);
+ DIRECTION_LIT_LEVEL.put(Side.RIGHT, 0.75f);
+ }
/* PROPERTIES */
protected byte _id = 0x0;
@@ -213,16 +224,23 @@ public void render() {
return;
if (_mesh == null) {
- if (getBlockForm() == BLOCK_FORM.BILLBOARD) {
- MeshCollection.addBillboardMesh(this, 1.0f, 1.0f);
- _mesh = Tessellator.getInstance().generateMesh();
- } else {
- MeshCollection.addBlockMesh(this, 1.0f, 1.0f, 0.9f);
- _mesh = Tessellator.getInstance().generateMesh();
-
+ Tessellator tessellator = new Tessellator();
+ tessellator.setColor(new Vector4f(1, 1, 1, 1));
+ if (_centerMesh != null)
+ {
+ tessellator.addMeshPart(_centerMesh);
}
-
- Tessellator.getInstance().resetAll();
+ for (Side dir : Side.values())
+ {
+ BlockMeshPart part = _sideMesh.get(dir);
+ if (part != null)
+ {
+ float lightLevel = DIRECTION_LIT_LEVEL.get(dir);
+ tessellator.setColor(new Vector4f(lightLevel, lightLevel, lightLevel, lightLevel));
+ tessellator.addMeshPart(part);
+ }
+ }
+ _mesh = tessellator.generateMesh();
}
if (getBlockForm() != BLOCK_FORM.BILLBOARD) {
View
8 src/org/terasology/model/inventory/ItemBlock.java
@@ -96,6 +96,10 @@ public boolean renderFirstPersonView() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glAlphaFunc(GL_GREATER, 0.1f);
+ if (activeBlock.isTranslucent())
+ {
+ glEnable(GL11.GL_ALPHA_TEST);
+ }
glPushMatrix();
@@ -108,6 +112,10 @@ public boolean renderFirstPersonView() {
glPopMatrix();
+ if (activeBlock.isTranslucent())
+ {
+ glDisable(GL11.GL_ALPHA_TEST);
+ }
glDisable(GL11.GL_BLEND);
ShaderManager.getInstance().enableShader(null);
View
30 src/org/terasology/model/shapes/BlockMeshPart.java
@@ -25,6 +25,36 @@ public BlockMeshPart(Vector3f[] vertices, Vector3f[] normals, Vector2f[] texCoor
_texCoords = Arrays.copyOf(texCoords, texCoords.length);
_indices = Arrays.copyOf(indices, indices.length);
}
+
+ public int size()
+ {
+ return _vertices.length;
+ }
+
+ public int indicesSize()
+ {
+ return _indices.length;
+ }
+
+ public Vector3f getVertex(int i)
+ {
+ return _vertices[i];
+ }
+
+ public Vector3f getNormal(int i)
+ {
+ return _normals[i];
+ }
+
+ public Vector2f getTexCoord(int i)
+ {
+ return _texCoords[i];
+ }
+
+ public int getIndex(int i)
+ {
+ return _indices[i];
+ }
public BlockMeshPart mapTexCoords(Vector2f offset, float width)
{
View
8 src/org/terasology/rendering/gui/components/UITransparentOverlay.java
@@ -18,7 +18,7 @@
import org.lwjgl.opengl.Display;
import org.terasology.rendering.gui.framework.UIDisplayElement;
import org.terasology.rendering.primitives.Mesh;
-import org.terasology.rendering.primitives.MeshCollection;
+import org.terasology.rendering.primitives.TessellatorHelper;
import org.terasology.rendering.primitives.Tessellator;
import javax.vecmath.Vector2f;
@@ -36,9 +36,9 @@
public UITransparentOverlay() {
if (_mesh == null) {
- MeshCollection.addGUIQuadMesh(new Vector4f(0.05f, 0.05f, 0.05f, 0.8f), 1.0f, 1.0f);
- _mesh = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Tessellator tessellator = new Tessellator();
+ TessellatorHelper.addGUIQuadMesh(tessellator, new Vector4f(0.05f, 0.05f, 0.05f, 0.8f), 1.0f, 1.0f);
+ _mesh = tessellator.generateMesh();
}
}
View
8 src/org/terasology/rendering/gui/framework/UIGraphicsElement.java
@@ -19,7 +19,7 @@
import org.lwjgl.opengl.GL11;
import org.terasology.logic.manager.TextureManager;
import org.terasology.rendering.primitives.Mesh;
-import org.terasology.rendering.primitives.MeshCollection;
+import org.terasology.rendering.primitives.TessellatorHelper;
import org.terasology.rendering.primitives.Tessellator;
import javax.vecmath.Vector2f;
@@ -44,9 +44,9 @@ public UIGraphicsElement(String textureName) {
_textureName = textureName;
if (_mesh == null) {
- MeshCollection.addGUIQuadMesh(new Vector4f(1f, 1f, 1f, 1f), 1.0f, 1.0f);
- _mesh = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Tessellator tessellator = new Tessellator();
+ TessellatorHelper.addGUIQuadMesh(tessellator, new Vector4f(1f, 1f, 1f, 1f), 1.0f, 1.0f);
+ _mesh = tessellator.generateMesh();
}
}
View
172 src/org/terasology/rendering/primitives/MeshCollection.java
@@ -1,172 +0,0 @@
-package org.terasology.rendering.primitives;
-
-
-import org.terasology.math.Side;
-import org.terasology.model.blocks.Block;
-
-import javax.vecmath.Vector2f;
-import javax.vecmath.Vector3f;
-import javax.vecmath.Vector4f;
-
-public class MeshCollection {
- public static void addBlockMesh(Vector4f color, float size, float light1, float light2, float posX, float posY, float posZ) {
- Vector2f defaultSize = new Vector2f(1.0f, 1.0f);
- Vector2f defaultOffset = new Vector2f(0.0f, 0.0f);
- addBlockMesh(color, defaultOffset, defaultSize, size, light1, light2, posX, posY, posZ);
- }
-
- public static void addBlockMesh(Vector4f color, Vector2f texOffset, Vector2f texSize, float size, float light1, float light2, float posX, float posY, float posZ) {
- Vector2f[] sizes = new Vector2f[6];
- Vector2f[] offsets = new Vector2f[6];
-
- for (int i = 0; i < 6; i++) {
- sizes[i] = texSize;
- offsets[i] = texOffset;
- }
-
- addBlockMesh(color, offsets, sizes, size, light1, light2, posX, posY, posZ);
- }
-
- public static void addBlockMesh(Vector4f color, Vector2f[] texOffsets, Vector2f[] texSizes, float size, float light1, float light2, float posX, float posY, float posZ) {
- final float sizeHalf = size / 2;
-
- Tessellator.getInstance().resetParams();
- Tessellator.getInstance().setColor(new Vector4f(light1 * color.x, light1 * color.y, light1 * color.z, color.w));
-
- Vector3f t1 = new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ);
- Vector3f t2 = new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ);
- Vector3f t3 = new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ);
- Vector3f t4 = new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[0].x, texOffsets[0].y));
- Tessellator.getInstance().addVertex(t1);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[0].x + texSizes[0].x, texOffsets[0].y));
- Tessellator.getInstance().addVertex(t2);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[0].x + texSizes[0].x, texOffsets[0].y + texSizes[0].y));
- Tessellator.getInstance().addVertex(t3);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[0].x, texOffsets[0].y + texSizes[0].y));
- Tessellator.getInstance().addVertex(t4);
-
- Vector3f l1 = new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ);
- Vector3f l2 = new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ);
- Vector3f l3 = new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ);
- Vector3f l4 = new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[1].x, texOffsets[1].y + texSizes[1].y));
- Tessellator.getInstance().addVertex(l1);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[1].x + texSizes[1].x, texOffsets[1].y + texSizes[1].y));
- Tessellator.getInstance().addVertex(l2);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[1].x + texSizes[1].x, texOffsets[1].y));
- Tessellator.getInstance().addVertex(l3);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[1].x, texOffsets[1].y));
- Tessellator.getInstance().addVertex(l4);
-
- Vector3f r1 = new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ);
- Vector3f r2 = new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ);
- Vector3f r3 = new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ);
- Vector3f r4 = new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[2].x, texOffsets[2].y));
- Tessellator.getInstance().addVertex(r1);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[2].x + texSizes[2].x, texOffsets[2].y));
- Tessellator.getInstance().addVertex(r2);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[2].x + texSizes[2].x, texOffsets[2].y + texSizes[2].y));
- Tessellator.getInstance().addVertex(r3);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[2].x, texOffsets[2].y + texSizes[2].y));
- Tessellator.getInstance().addVertex(r4);
-
- Tessellator.getInstance().setColor(new Vector4f(light2 * color.x, light2 * color.y, light2 * color.z, color.w));
-
- Vector3f f1 = new Vector3f(-sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ);
- Vector3f f2 = new Vector3f(sizeHalf + posX, sizeHalf + posY, -sizeHalf + posZ);
- Vector3f f3 = new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ);
- Vector3f f4 = new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[3].x, texOffsets[3].y));
- Tessellator.getInstance().addVertex(f1);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[3].x + texSizes[3].x, texOffsets[3].y));
- Tessellator.getInstance().addVertex(f2);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[3].x + texSizes[3].x, texOffsets[3].y + texSizes[3].y));
- Tessellator.getInstance().addVertex(f3);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[3].x, texOffsets[3].y + texSizes[3].y));
- Tessellator.getInstance().addVertex(f4);
-
- Vector3f b1 = new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ);
- Vector3f b2 = new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ);
- Vector3f b3 = new Vector3f(sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ);
- Vector3f b4 = new Vector3f(-sizeHalf + posX, sizeHalf + posY, sizeHalf + posZ);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[4].x, texOffsets[4].y + texSizes[4].y));
- Tessellator.getInstance().addVertex(b1);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[4].x + texSizes[4].x, texOffsets[4].y + texSizes[4].y));
- Tessellator.getInstance().addVertex(b2);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[4].x + texSizes[4].x, texOffsets[4].y));
- Tessellator.getInstance().addVertex(b3);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[4].x, texOffsets[4].y));
- Tessellator.getInstance().addVertex(b4);
-
- Vector3f bo1 = new Vector3f(-sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ);
- Vector3f bo2 = new Vector3f(sizeHalf + posX, -sizeHalf + posY, -sizeHalf + posZ);
- Vector3f bo3 = new Vector3f(sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ);
- Vector3f bo4 = new Vector3f(-sizeHalf + posX, -sizeHalf + posY, sizeHalf + posZ);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[5].x, texOffsets[5].y));
- Tessellator.getInstance().addVertex(bo1);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[5].x + texSizes[5].x, texOffsets[5].y));
- Tessellator.getInstance().addVertex(bo2);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[5].x + texSizes[5].x, texOffsets[5].y + texSizes[5].y));
- Tessellator.getInstance().addVertex(bo3);
- Tessellator.getInstance().setTex(new Vector2f(texOffsets[5].x, texOffsets[5].y + texSizes[5].y));
- Tessellator.getInstance().addVertex(bo4);
- }
-
- public static void addBlockMesh(Block block, float size, float light1, float light2) {
- Vector2f[] sizes = new Vector2f[6];
- Vector2f[] offsets = new Vector2f[6];
-
- for (int i = 0; i < 6; i++) {
- sizes[i] = new Vector2f(Block.TEXTURE_OFFSET_WIDTH, Block.TEXTURE_OFFSET_WIDTH);
- offsets[i] = new Vector2f(block.calcTextureOffsetFor(Side.values()[i]));
- }
-
- addBlockMesh(new Vector4f(1.0f, 1.0f, 1.0f, 1.0f), offsets, sizes, size, light1, light2, 0, 0, 0);
- }
-
- public static void addGUIQuadMesh(Vector4f color, float sizeX, float sizeY) {
- Tessellator.getInstance().resetParams();
- Tessellator.getInstance().setColor(new Vector4f(color.x, color.y, color.z, color.w));
-
- Vector3f t1 = new Vector3f(0, 0, 0);
- Vector3f t2 = new Vector3f(sizeX, 0, 0);
- Vector3f t3 = new Vector3f(sizeX, sizeY, 0);
- Vector3f t4 = new Vector3f(0, sizeY, 0);
- Tessellator.getInstance().setTex(new Vector2f(0, 0));
- Tessellator.getInstance().addVertex(t1);
- Tessellator.getInstance().setTex(new Vector2f(1, 0));
- Tessellator.getInstance().addVertex(t2);
- Tessellator.getInstance().setTex(new Vector2f(1, 1));
- Tessellator.getInstance().addVertex(t3);
- Tessellator.getInstance().setTex(new Vector2f(0, 1));
- Tessellator.getInstance().addVertex(t4);
-
- }
-
- public static void addBillboardMesh(Block block, float size, float light1) {
- final float sizeHalf = size / 2;
-
- Tessellator.getInstance().resetParams();
- Tessellator.getInstance().setColor(new Vector4f(light1, light1, light1, 1.0f));
-
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.LEFT).x, block.calcTextureOffsetFor(Side.LEFT).y + Block.TEXTURE_OFFSET_WIDTH));
- Tessellator.getInstance().addVertex(new Vector3f(0f, -sizeHalf, -sizeHalf));
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.LEFT).x + Block.TEXTURE_OFFSET_WIDTH, block.calcTextureOffsetFor(Side.LEFT).y + Block.TEXTURE_OFFSET_WIDTH));
- Tessellator.getInstance().addVertex(new Vector3f(0f, -sizeHalf, sizeHalf));
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.LEFT).x + Block.TEXTURE_OFFSET_WIDTH, block.calcTextureOffsetFor(Side.LEFT).y));
- Tessellator.getInstance().addVertex(new Vector3f(0f, sizeHalf, sizeHalf));
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.LEFT).x, block.calcTextureOffsetFor(Side.LEFT).y));
- Tessellator.getInstance().addVertex(new Vector3f(0f, sizeHalf, -sizeHalf));
-
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.BACK).x, block.calcTextureOffsetFor(Side.BACK).y + Block.TEXTURE_OFFSET_WIDTH));
- Tessellator.getInstance().addVertex(new Vector3f(-sizeHalf, -sizeHalf, 0f));
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.BACK).x + Block.TEXTURE_OFFSET_WIDTH, block.calcTextureOffsetFor(Side.BACK).y + Block.TEXTURE_OFFSET_WIDTH));
- Tessellator.getInstance().addVertex(new Vector3f(sizeHalf, -sizeHalf, 0f));
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.BACK).x + Block.TEXTURE_OFFSET_WIDTH, block.calcTextureOffsetFor(Side.BACK).y));
- Tessellator.getInstance().addVertex(new Vector3f(sizeHalf, sizeHalf, 0f));
- Tessellator.getInstance().setTex(new Vector2f(block.calcTextureOffsetFor(Side.BACK).x, block.calcTextureOffsetFor(Side.BACK).y));
- Tessellator.getInstance().addVertex(new Vector3f(-sizeHalf, sizeHalf, 0f));
- }
-}
View
8 src/org/terasology/rendering/primitives/MeshFactory.java
@@ -10,6 +10,7 @@
/* SINGLETON */
private static MeshFactory _instance;
+ private Tessellator tessellator = new Tessellator();
public static MeshFactory getInstance() {
if (_instance == null)
@@ -22,6 +23,7 @@ private MeshFactory() {
}
public Mesh generateItemMesh(int posX, int posY) {
+
TextureManager.Texture tex = TextureManager.getInstance().getTexture("items");
ByteBuffer buffer = tex.data;
@@ -38,13 +40,13 @@ public Mesh generateItemMesh(int posX, int posY) {
int a = buffer.get((posY + y) * stride + (posX + x) * 4 + 3) & 255;
if (a != 0) {
- MeshCollection.addBlockMesh(new Vector4f(r / 255f, g / 255f, b / 255f, 1.0f), 2f * 0.0625f, 1.0f, 0.5f, 2f * 0.0625f * x - 1f / 2f, 2f * 0.0625f * (16 - y) - 1f, 0f);
+ TessellatorHelper.addBlockMesh(tessellator, new Vector4f(r / 255f, g / 255f, b / 255f, 1.0f), 2f * 0.0625f, 1.0f, 0.5f, 2f * 0.0625f * x - 1f / 2f, 2f * 0.0625f * (16 - y) - 1f, 0f);
}
}
}
- Mesh result = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Mesh result = tessellator.generateMesh();
+ tessellator.resetAll();
return result;
}
View
131 src/org/terasology/rendering/primitives/Tessellator.java
@@ -1,9 +1,14 @@
package org.terasology.rendering.primitives;
+import gnu.trove.iterator.TIntIterator;
+import gnu.trove.list.TFloatList;
+import gnu.trove.list.TIntList;
import gnu.trove.list.array.TFloatArrayList;
+import gnu.trove.list.array.TIntArrayList;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL15;
import org.terasology.logic.manager.VertexBufferObjectManager;
+import org.terasology.model.shapes.BlockMeshPart;
import javax.vecmath.Vector2f;
import javax.vecmath.Vector3f;
@@ -13,28 +18,21 @@
public class Tessellator {
- /* SINGLETON */
- private static Tessellator _instance;
-
private TFloatArrayList _color = new TFloatArrayList();
private TFloatArrayList _vertices = new TFloatArrayList();
private TFloatArrayList _tex = new TFloatArrayList();
private TFloatArrayList _normals = new TFloatArrayList();
+ private TIntArrayList _indices = new TIntArrayList();
+ private int _indexOffset = 0;
private Vector4f _activeColor = new Vector4f();
private Vector3f _activeNormal = new Vector3f();
private Vector2f _activeTex = new Vector2f();
private Vector3f _lighting = new Vector3f();
- public static Tessellator getInstance() {
- if (_instance == null)
- _instance = new Tessellator();
-
- return _instance;
- }
- private Tessellator() {
- resetAll();
+ public Tessellator() {
+ resetParams();
}
public void resetAll() {
@@ -42,6 +40,8 @@ public void resetAll() {
_vertices.reset();
_tex.reset();
_normals.reset();
+ _indices.reset();
+ _indexOffset = 0;
resetParams();
}
@@ -52,28 +52,82 @@ public void resetParams() {
_lighting.set(1, 1, 1);
_activeNormal.set(0, 1, 0);
}
+
+ public void addPoly(Vector3f[] vertices, Vector2f[] texCoords)
+ {
+ if (vertices.length != texCoords.length || vertices.length < 3)
+ {
+ throw new IllegalArgumentException("addPoly expected vertices.length == texCoords.length > 2");
+ }
+
+ for (int i = 0; i < vertices.length; ++i)
+ {
+ _vertices.add(vertices[i].x);
+ _vertices.add(vertices[i].y);
+ _vertices.add(vertices[i].z);
- public void addVertex(Vector3f v) {
- _vertices.add(v.x);
- _vertices.add(v.y);
- _vertices.add(v.z);
+ _color.add(_activeColor.x);
+ _color.add(_activeColor.y);
+ _color.add(_activeColor.z);
+ _color.add(_activeColor.w);
- _color.add(_activeColor.x);
- _color.add(_activeColor.y);
- _color.add(_activeColor.z);
- _color.add(_activeColor.w);
+ _normals.add(_activeNormal.x);
+ _normals.add(_activeNormal.y);
+ _normals.add(_activeNormal.z);
- _normals.add(_activeNormal.x);
- _normals.add(_activeNormal.y);
- _normals.add(_activeNormal.z);
+ _tex.add(texCoords[i].x);
+ _tex.add(texCoords[i].y);
+ _tex.add(0.0f);
- _tex.add(_activeTex.x);
- _tex.add(_activeTex.y);
- _tex.add(0.0f);
+ _tex.add(_lighting.x);
+ _tex.add(_lighting.y);
+ _tex.add(_lighting.z);
+ }
- _tex.add(_lighting.x);
- _tex.add(_lighting.y);
- _tex.add(_lighting.z);
+ // Standard fan
+ for (int i = 0; i < vertices.length - 2; i++)
+ {
+ _indices.add(_indexOffset);
+ _indices.add(_indexOffset + i + 1);
+ _indices.add(_indexOffset + i + 2);
+ }
+ _indexOffset += vertices.length;
+ }
+
+ public void addMeshPart(BlockMeshPart part)
+ {
+ for (int i = 0; i < part.size(); ++i)
+ {
+ Vector3f vertex = part.getVertex(i);
+ _vertices.add(vertex.x);
+ _vertices.add(vertex.y);
+ _vertices.add(vertex.z);
+
+ _color.add(_activeColor.x);
+ _color.add(_activeColor.y);
+ _color.add(_activeColor.z);
+ _color.add(_activeColor.w);
+
+ Vector3f normal = part.getNormal(i);
+ _normals.add(normal.x);
+ _normals.add(normal.y);
+ _normals.add(normal.z);
+
+ Vector2f uv = part.getTexCoord(i);
+ _tex.add(uv.x);
+ _tex.add(uv.y);
+ _tex.add(0);
+
+ _tex.add(_lighting.x);
+ _tex.add(_lighting.y);
+ _tex.add(_lighting.z);
+ }
+
+ for (int i = 0; i < part.indicesSize(); ++i)
+ {
+ _indices.add(_indexOffset + part.getIndex(i));
+ }
+ _indexOffset += part.size();
}
public void setColor(Vector4f v) {
@@ -109,23 +163,12 @@ private FloatBuffer createCombinedBuffer() {
}
private IntBuffer createIndexBuffer() {
- IntBuffer indices = BufferUtils.createIntBuffer(((_vertices.size() / 3) / 4) * 6);
-
- int idx = 0;
- for (int i = 0; i < _vertices.size(); i += 3) {
- if (i % 4 == 0) {
- indices.put(idx);
- indices.put(idx + 1);
- indices.put(idx + 2);
-
- indices.put(idx + 2);
- indices.put(idx + 3);
- indices.put(idx);
-
- idx += 4;
- }
+ IntBuffer indices = BufferUtils.createIntBuffer(_indices.size());
+ TIntIterator iterator = _indices.iterator();
+ while (iterator.hasNext())
+ {
+ indices.put(iterator.next());
}
-
indices.flip();
return indices;
}
View
162 src/org/terasology/rendering/primitives/TessellatorHelper.java
@@ -0,0 +1,162 @@
+package org.terasology.rendering.primitives;
+
+
+import org.terasology.math.Side;
+import org.terasology.model.blocks.Block;
+
+import javax.vecmath.Vector2f;
+import javax.vecmath.Vector3f;
+import javax.vecmath.Vector4f;
+
+public class TessellatorHelper {
+ public static void addBlockMesh(Tessellator tessellator, Vector4f color, float size, float light1, float light2, float posX, float posY, float posZ) {
+ Vector2f defaultSize = new Vector2f(1.0f, 1.0f);
+ Vector2f defaultOffset = new Vector2f(0.0f, 0.0f);
+ addBlockMesh(tessellator, color, defaultOffset, defaultSize, size, light1, light2, posX, posY, posZ);
+ }
+
+ public static void addBlockMesh(Tessellator tessellator, Vector4f color, Vector2f texOffset, Vector2f texSize, float size, float light1, float light2, float posX, float posY, float posZ) {
+ Vector2f[] sizes = new Vector2f[6];
+ Vector2f[] offsets = new Vector2f[6];
+
+ for (int i = 0; i < 6; i++) {
+ sizes[i] = texSize;
+ offsets[i] = texOffset;
+ }
+
+ addBlockMesh(tessellator, color, offsets, sizes, size, light1, light2, posX, posY, posZ);
+ }
+
+ 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)
+ });
+ }
+
+ public static void addGUIQuadMesh(Tessellator tessellator, Vector4f color, float sizeX, float sizeY) {
+ tessellator.resetParams();
+ tessellator.setColor(new Vector4f(color.x, color.y, color.z, color.w));
+
+ tessellator.addPoly(
+ new Vector3f[] {
+ new Vector3f(0, 0, 0),
+ new Vector3f(sizeX, 0, 0),
+ new Vector3f(sizeX, sizeY, 0),
+ new Vector3f(0, sizeY, 0)
+ },
+ new Vector2f[] {
+ new Vector2f(0, 0),
+ new Vector2f(1, 0),
+ new Vector2f(1, 1),
+ new Vector2f(0, 1)
+ }
+ );
+ }
+
+}
View
8 src/org/terasology/rendering/world/BlockGrid.java
@@ -20,7 +20,7 @@
import org.terasology.model.structures.BlockPosition;
import org.terasology.rendering.interfaces.IGameObject;
import org.terasology.rendering.primitives.Mesh;
-import org.terasology.rendering.primitives.MeshCollection;
+import org.terasology.rendering.primitives.TessellatorHelper;
import org.terasology.rendering.primitives.Tessellator;
import javax.vecmath.Vector3d;
@@ -45,9 +45,9 @@
public BlockGrid(WorldRenderer parent) {
_parent = parent;
- MeshCollection.addBlockMesh(new Vector4f(0.0f, 0.0f, 1.0f, 0.25f), 1.005f, 1.0f, 0.5f, 0.0f, 0.0f, 0.0f);
- _mesh = Tessellator.getInstance().generateMesh();
- Tessellator.getInstance().resetAll();
+ Tessellator tessellator = new Tessellator();
+ TessellatorHelper.addBlockMesh(tessellator, new Vector4f(0.0f, 0.0f, 1.0f, 0.25f), 1.005f, 1.0f, 0.5f, 0.0f, 0.0f, 0.0f);
+ _mesh = tessellator.generateMesh();
}
public void render() {
Please sign in to comment.
Something went wrong with that request. Please try again.