public void addModelToScene() {
OctreeRoot octree = block.getOctree();
Mesh mesh = new Mesh();
MeshData meshData = mesh.getMeshData();
int numOfQuads = quads.size();
int numOfVoxels = voxelCounter;
FloatBuffer vertexBuffer = BufferUtils.createVector3Buffer(numOfVoxels);
IntBuffer indexBuffer = BufferUtils.createIntBuffer(numOfQuads * 6);
FloatBuffer normalBuffer = BufferUtils.createVector3Buffer(numOfVoxels);
// FloatBuffer texCoordBuffer = BufferUtils.createVector2Buffer(numOfVoxels);
FloatBuffer colorBuffer = BufferUtils.createColorBuffer(numOfVoxels);
ArrayList<Integer> verticesWithGrass = new ArrayList<Integer>();
int blockSize = VoxelWorld.BLOCK_SIZE;
for (int x = 0; x < blockSize + 1; x++) {
for (int y = 0; y < blockSize + 1; y++) {
for (int z = 0; z < blockSize + 1; z++) {
Voxel voxel = voxels[x][y][z];
if (voxel != null) {
voxel.summarize();
octree.insertVertex(x, y, z, voxel.index);
int i3 = voxel.index * 3;
vertexBuffer.put(i3+0, voxel.vertexPosition.getXf());
vertexBuffer.put(i3+1, voxel.vertexPosition.getYf());
vertexBuffer.put(i3+2, voxel.vertexPosition.getZf());
Vector3 normal = computeVoxelNormal(x,y,z,voxel.vertexPosition);
normalBuffer.put(i3+0, normal.getXf());
normalBuffer.put(i3+1, normal.getYf());
normalBuffer.put(i3+2, normal.getZf());
int i4 = voxel.index * 4;
colorBuffer.put(i4+0, voxel.materials.getXf());
colorBuffer.put(i4+1, voxel.materials.getYf());
colorBuffer.put(i4+2, voxel.materials.getZf());
colorBuffer.put(i4+3, voxel.materials.getWf());
///GRASS
if (voxel.vertexPosition.getY() > 33 && voxel.vertexPosition.getYf() < 210 && normal.getY() > 0.7)
verticesWithGrass.add(voxel.index);
}
}
}
}
for (int q = 0; q < quads.size(); q++) {
Quad quad = quads.get(q);
indexBuffer.put(quad.voxel1.index);
indexBuffer.put(quad.voxel2.index);
indexBuffer.put(quad.voxel3.index);
indexBuffer.put(quad.voxel1.index);
indexBuffer.put(quad.voxel3.index);
indexBuffer.put(quad.voxel4.index);
}
indexBuffer.rewind();
//colorBuffer.flip();
meshData.setVertexBuffer(vertexBuffer);
meshData.setIndexBuffer(indexBuffer);
meshData.setNormalBuffer(normalBuffer);
meshData.setColorBuffer(colorBuffer);
// meshData.setColorBuffer(colorBuffer);
block.numOfTriangles = quads.size()*2;
block.numOfVertices = voxelCounter;
Vector3 center = basePosition;
double halfSize = pool.getLevel().getScale() * VoxelWorld.BLOCK_SIZE / 2.0;
center.addLocal(halfSize, halfSize, halfSize);
mesh.setModelBound(new BoundingBox(center, halfSize, halfSize, halfSize));
block.getVoxelNode().attachChild(mesh);
//////////GRASS///////////////////
if (pool.getLevel().getLevelID() == 0) {
int numOfGrasses = verticesWithGrass.size();
Mesh grassMesh = new Mesh();
MeshData grassMeshData = grassMesh.getMeshData();
FloatBuffer grassVertexBuffer = BufferUtils.createVector3Buffer(numOfGrasses * 4);
IntBuffer grassIndexBuffer = BufferUtils.createIntBuffer(numOfGrasses * 6);
FloatBuffer grassNormalBuffer = BufferUtils.createVector3Buffer(numOfGrasses * 4);
FloatBuffer grassTexCoordBuffer = BufferUtils.createVector2Buffer(numOfGrasses * 4);
//FloatBuffer grassBasePositionBuffer = BufferUtils.createVector2Buffer(numOfGrasses * 4);
for (int i=0, size = verticesWithGrass.size(); i<size; i++) {
int i3 = verticesWithGrass.get(i) * 3;
int i4 = i * 4;
float topY = 2.5f + (float) Math.random();
float bottomY = -0.1f;
float halfEidth = 1.5f + (float) Math.random();
//double alpha = Math.random()*Math.PI;
//float offsetX = (FastMath.sin(alpha) * halfEidth);
//float offsetZ = (float)(FastMath.cos(alpha) * halfEidth);
float offsetX = (float)Math.random()-0.5f;
float offsetZ = (float)Math.random()-0.5f;
grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
/*grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
grassVertexBuffer.put(vertexBuffer.get(i3 +0) + offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) + offsetZ);
grassVertexBuffer.put(vertexBuffer.get(i3 +0) - offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + topY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) - offsetZ);
grassVertexBuffer.put(vertexBuffer.get(i3 +0) - offsetX);
grassVertexBuffer.put(vertexBuffer.get(i3 +1) + bottomY);
grassVertexBuffer.put(vertexBuffer.get(i3 +2) - offsetZ);*/
float nx = normalBuffer.get(i3 +0);
float ny = normalBuffer.get(i3 +1);
float nz = normalBuffer.get(i3 +2);
grassNormalBuffer.put(nx);
grassNormalBuffer.put(ny);
grassNormalBuffer.put(nz);
grassNormalBuffer.put(nx);
grassNormalBuffer.put(ny);
grassNormalBuffer.put(nz);
grassNormalBuffer.put(nx);
grassNormalBuffer.put(ny);
grassNormalBuffer.put(nz);
grassNormalBuffer.put(nx);
grassNormalBuffer.put(ny);
grassNormalBuffer.put(nz);
grassTexCoordBuffer.put(0);
grassTexCoordBuffer.put(0);
grassTexCoordBuffer.put(0);
grassTexCoordBuffer.put(1);
grassTexCoordBuffer.put(1);
grassTexCoordBuffer.put(1);
grassTexCoordBuffer.put(1);
grassTexCoordBuffer.put(0);
grassIndexBuffer.put(i4);
grassIndexBuffer.put(i4+1);
grassIndexBuffer.put(i4+2);
grassIndexBuffer.put(i4);
grassIndexBuffer.put(i4+2);
grassIndexBuffer.put(i4+3);
}
//grassIndexBuffer.rewind();
//colorBuffer.flip();
grassMeshData.setVertexBuffer(grassVertexBuffer);
grassMeshData.setIndexBuffer(grassIndexBuffer);
grassMeshData.setNormalBuffer(grassNormalBuffer);
grassMeshData.setTextureBuffer(grassTexCoordBuffer,0);
grassMesh.setModelBound(new BoundingBox(center, halfSize, halfSize, halfSize));
block.getGrassNode().attachChild(grassMesh);
}