ArrayList<Float> idBuffer = new ArrayList<>();
Matrix4f transMat = new Matrix4f();
Matrix4f rotMat = new Matrix4f();
Vector4f translatedVertex = new Vector4f();
Vector4f rotatedNormal = new Vector4f();
float[] temp = new float[9];
for(int row = 0; row < Map.chunkSize; row++) {
for(int col = 0; col < Map.chunkSize; col++) {
int x = xStart+col;
int y = yStart+row;
if(map.get(y, x) == Map.EMPTY){
/* Nothing to be added*/
continue;
}
ArrayList<Mesh> msh = new ArrayList<>();
int scan[] = map.scan(y, x);
float mRotation = 0f;
/* Set transform matrix for this piece of track */
transMat.setIdentity();
rotMat.setIdentity();
/* Translate by x,z coordinates */
transMat.translate(new Vector3f(y, 0, x));
int trackType = getTrackType(map.get(y, x), scan, map.scanRideable(y, x));
/* Populate msh list with right .obj meshes */
switch(trackType){
case EW_STRAIGHT:
mRotation -= 90f;
rotMat.rotate((float) Math.toRadians(mRotation), new Vector3f(0, 1, 0));
case NS_STRAIGHT:
//msh.add(trackStraight);
msh.add(objMeshes.get("res/mesh/track.obj"));
break;
case NSEW_CROSS:
//msh.add(trackXCrossing);
msh.add(objMeshes.get("res/mesh/track_x_cross.obj"));
break;
case NE_CURVE:
mRotation -= 90f;
case NW_CURVE:
mRotation -= 90f;
case SW_CURVE:
mRotation -= 90f;
rotMat.rotate((float) Math.toRadians(mRotation), new Vector3f(0, 1, 0));
case SE_CURVE:
//msh.add(track90Corner);
msh.add(objMeshes.get("res/mesh/track_90_corner.obj"));
break;
case WNE_CROSS:
mRotation -= 90f;
case SWN_CROSS:
mRotation -= 90f;
case ESW_CROSS:
mRotation -= 90f;
rotMat.rotate((float) Math.toRadians(mRotation), new Vector3f(0, 1, 0));
case NES_CROSS:
//msh.add(trackTCrossing);
msh.add(objMeshes.get("res/mesh/track_t_cross.obj"));
break;
case EW_STATION:
mRotation -= 90f;
rotMat.rotate((float) Math.toRadians(mRotation), new Vector3f(0, 1, 0));
case NS_STATION:
//msh.add(trackStraight);
//msh.add(station);
msh.add(objMeshes.get("res/mesh/track.obj"));
msh.add(objMeshes.get("res/mesh/station2.obj"));
if(rand.nextFloat() > 0.5f){
//msh.add(station_bin);
msh.add(objMeshes.get("res/mesh/station2_bin.obj"));
}
if(rand.nextFloat() > 0.5f){
//msh.add(station_bench);
msh.add(objMeshes.get("res/mesh/station2_bench.obj"));
}
if(rand.nextFloat() > 0.75f){
//msh.add(station_map);
msh.add(objMeshes.get("res/mesh/station2_map.obj"));
}
break;
case WNE_STATION:
mRotation -= 90f;
case SWN_STATION:
mRotation -= 90f;
case ESW_STATION:
mRotation -= 90f;
rotMat.rotate((float) Math.toRadians(mRotation), new Vector3f(0, 1, 0));
case NES_STATION:
//msh.add(trackTCrossing);
//msh.add(station);
msh.add(objMeshes.get("res/mesh/track_t_cross.obj"));
msh.add(objMeshes.get("res/mesh/station2.obj"));
if(rand.nextFloat() > 0.5f){
//msh.add(station_bin);
msh.add(objMeshes.get("res/mesh/station2_bin.obj"));
}
if(rand.nextFloat() > 0.5f){
//msh.add(station_bench);
msh.add(objMeshes.get("res/mesh/station2_bench.obj"));
}
if(rand.nextFloat() > 0.75f){
//msh.add(station_map);
msh.add(objMeshes.get("res/mesh/station2_map.obj"));
}
break;
case TREE:
transMat.translate(new Vector3f(((float)Math.random()-0.5f)*0.8f, 0, ((float)Math.random()-0.5f)*0.8f));
rotMat.rotate((float) Math.toRadians(Math.random()*360.0), new Vector3f(0, 1, 0));
msh.add(objMeshes.get("res/mesh/cactus_trunk.obj"));
if(rand.nextFloat() > 0.15f){
msh.add(objMeshes.get("res/mesh/cactus_branch1.obj"));
}
if(rand.nextFloat() > 0.15f){
msh.add(objMeshes.get("res/mesh/cactus_branch2.obj"));
}
if(rand.nextFloat() > 0.15f){
msh.add(objMeshes.get("res/mesh/cactus_branch3.obj"));
}
break;
default:
//msh.add(trackStraight);
msh.add(objMeshes.get("res/mesh/track.obj"));
}
/* Stitch together the meshes of current grid */
for(int i=0; i<msh.size(); i++){
FloatBuffer data = msh.get(i).getData();
int indices = msh.get(i).getIndiceCount();
int keyframes = msh.get(i).getKeyframeCount();
int[] offsets = new int[4];
offsets[0] = 0; // always only the first keyframe
offsets[1] = 3*indices*keyframes;
offsets[2] = 2*indices+offsets[1];
offsets[3] = 3*indices*keyframes+offsets[2];
data.rewind();
for(int n=0;n<indices;n++){
data.position(offsets[0]);
data.get(temp, 0, 3);
data.position(offsets[1]);
data.get(temp, 3, 2);
data.position(offsets[2]);
data.get(temp, 5, 3);
data.position(offsets[3]);
data.get(temp, 8, 1);
offsets[0] += 3;
offsets[1] += 2;
offsets[2] += 3;
offsets[3]++;
/* XYZ */
Matrix4f.transform(rotMat, new Vector4f(temp[0], temp[1], temp[2], 1), translatedVertex);
Matrix4f.transform(transMat, translatedVertex, translatedVertex);
posBuffer.add(translatedVertex.x);
posBuffer.add(translatedVertex.y);
posBuffer.add(translatedVertex.z);
/* UV */
uvBuffer.add(temp[3]);
uvBuffer.add(temp[4]);
/* Normals */
Matrix4f.transform(rotMat, new Vector4f(temp[5], temp[6], temp[7], 0), rotatedNormal);
norBuffer.add(rotatedNormal.x);
norBuffer.add(rotatedNormal.y);
norBuffer.add(rotatedNormal.z);