package com.lloydm.geosword;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Random;

import com.lloydm.geosword1.R;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.GLSLShader;
import com.threed.jpct.Loader;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.ExtendedPrimitives;
import com.threed.jpct.util.MemoryHelper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

public class J3D
{
     public FrameLayout flay = null;
     private GLSurfaceView glview;
     private FrameBuffer fbuffer;
     private World world = null;

     // private long stime = 0;

     public static boolean isHD = true; // approximate - used to determine texture sizes to use...(for certain effects)

     private Random irand = null;

     public boolean viewmode = false;

     public MediaPlayer musicplayer = null;
     private volatile boolean cnotsetyet = true;
     public volatile boolean musicisreadytoplay = false;
     public volatile boolean musicstartedplaying = false;
     private volatile boolean usecustommusic = false;
     private String custommusicfilename = "";

     private SimpleVector uu = null;
     private SimpleVector vv = null;
     private SimpleVector ww = null;
     private SimpleVector yy = null;
    
     public volatile float maxmusicvolume = 0.65f;

     private boolean musicflag = true;

     // multipass rendering....
     private World skyboxworld;
     private Object3D[] skybox;
     private Camera skyboxcam;

     private SimpleVector v1, v2;

     private World uiworld;
     private Camera uicam;
     // private int ui_i = 0;

     public SeekBar guiseektime = null;
     public SeekBar loadingbar = null;
     public TextView txtloadstatus = null;
     public Button mediaplaypause = null;
     public Button mediatakepicture = null;

     public Button btnhighlight = null;
     private volatile boolean highlight = false;

     public TextView attcallsign = null;
     public TextView defcallsign = null;

     private String attackercallsign = "";
     private String defendercallsign = "";

     public volatile int loadingvalue = 1;
     public volatile int loadingprogress = 1;
     public volatile long seekbarhidetime = -1;
     public volatile boolean startedreplay = false;
     public volatile boolean sliderstatus = false;
     public volatile boolean replaymode = true;
     public volatile boolean ijustpaused = false;

     public volatile boolean takeshot = false;
     public volatile boolean takingshot = false;

     public volatile boolean takevideo = false;
     public volatile boolean takingvideo = false;
     public volatile int takingvideoframe = 0;

     public volatile boolean doupdateframe = false;

     // shaders.....
     private GLSLShader shaderbasic;
     private GLSLShader shaderlighting;
     private GLSLShader shaderlightingbump;
     private GLSLShader shaderbasicwcolor;
     private GLSLShader shaderlightingbumpwcolor;
     private GLSLShader shadercorona;
     private GLSLShader shaderbasiccoloronly;

     private GLSLShader shaderbeambisect;

     private int attplanetid = -1;

     private GLSLShader[] shaders;

     private int[] glpixels = null;

     private boolean firstupdate = true;
     private boolean dodraw = false;

     private MyRenderer renderer;

     public Level3 currentlevel;

     public String replayfile = null;

     private final static String TAG = "com.lloydm.geosword.J3D";
     private Context context;
     public AssetManager assets;
     public Camera camera;
     public int cframe = 1;

     private long starttime = 0;
     private long timetoready = 0;

     private int skipnum = 0;
     private long frametime = 0;

     public J3D(AssetManager assets)
     {
                     this.assets = assets;
                     irand = new Random();
                     irand.setSeed(System.currentTimeMillis());
     }

     public J3D(AssetManager assets, boolean viewmode)
     {
                     this.assets = assets;
                     this.viewmode = true;
                     irand = new Random();
                     irand.setSeed(System.currentTimeMillis());
     }

     public J3D(AssetManager assets, String rfile)
     {
                     this.assets = assets;
                     this.replayfile = rfile;
                     irand = new Random();
                     irand.setSeed(System.currentTimeMillis());
     }

     public J3D(AssetManager assets, String rfile, int attplanetid, boolean musicflag)
     {
                     this.assets = assets;
                     this.replayfile = rfile;
                     irand = new Random();
                     irand.setSeed(System.currentTimeMillis());
                     this.attplanetid = attplanetid;
                     this.musicflag = musicflag;
     }

     public J3D(AssetManager assets, String rfile, int attplanetid, boolean musicflag, String attackername, String defendername)
     {
                     this.assets = assets;
                     this.replayfile = rfile;
                     irand = new Random();
                     irand.setSeed(System.currentTimeMillis());
                     this.attplanetid = attplanetid;
                     this.musicflag = musicflag;
                     this.attackercallsign = attackername;
                     this.defendercallsign = defendername;
     }

     public void
setBackground()
     {
                     if (glview != null)
                     {
                                     ((Activity) this.context).runOnUiThread(new Runnable()
                                     {

                                                     @Override
                                                     public void
run()
                                                     {

                                                                     glview.setBackgroundResource(R.drawable.loading1bk);
                                                     }
                                     })
;
                     }
     }

     public void setLoadingBar(final int val)
     {
                     ((Activity) this.context).runOnUiThread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     if (loadingbar != null)
                                                     {
                                                                     loadingbar.setProgress(val);
                                                     }

                                     }
                     })
;
     }

     public void setLoadingStatus(final int val)
     {
                     ((Activity) this.context).runOnUiThread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     if (txtloadstatus != null)
                                                     {
                                                                     if (val == 0) // still reading file
                                                                     {
                                                                                     txtloadstatus.setText("Reading Replay File");

                                                                     }
                                                                     else
                                                                     // preparing 3d environment.
                                                                     {
                                                                                     if (val == 2)
                                                                                     {
                                                                                                     txtloadstatus.setText("Preparing 3D Environment..35%");
                                                                                     }
                                                                                     if (val == 13)
                                                                                     {
                                                                                                     txtloadstatus.setText("Preparing 3D Environment..80%");
                                                                                     }
                                                                                     if (val == 100)
                                                                                     {
                                                                                                     txtloadstatus.setText("Preparing 3D Environment 99%");
                                                                                     }
                                                                                     if (val != 2 && val != 13 && val != 100)
                                                                                     {
                                                                                                     txtloadstatus.setText("Preparing 3D Environment.." + val);
                                                                                     }

                                                                     }
                                                     }

                                     }
                     });
     }

     public void enabledHUD()
     {
                     // if(sliderstatus)
                     // {
                     // return;
                     // }
                     ((Activity) this.context).runOnUiThread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     if (attcallsign != null)
                                                     {
                                                                     attcallsign.bringToFront();
                                                                     attcallsign.setVisibility(View.VISIBLE);
                                                                     try
                                                                     {
                                                                                     flay.requestLayout();
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Exception in layout request");
                                                                     }
                                                                     flay.invalidate();
                                                     }
                                                     if (defcallsign != null)
                                                     {
                                                                     defcallsign.bringToFront();
                                                                     defcallsign.setVisibility(View.VISIBLE);
                                                                     try
                                                                     {
                                                                                     flay.requestLayout();
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Exception in layout request");
                                                                     }
                                                                     flay.invalidate();
                                                     }
                                                     if (guiseektime != null)
                                                     {
                                                                     guiseektime.bringToFront();
                                                                     guiseektime.setEnabled(true);
                                                                     guiseektime.setVisibility(View.VISIBLE);
                                                                     try
                                                                     {
                                                                                     flay.requestLayout();
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Exception in layout request");
                                                                     }
                                                                     flay.invalidate();
                                                     }
                                                     if (btnhighlight != null)
                                                     {
                                                                     btnhighlight.bringToFront();
                                                                     btnhighlight.setEnabled(true);
                                                                     btnhighlight.setVisibility(View.VISIBLE);
                                                                     try
                                                                     {
                                                                                     flay.requestLayout();
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Exception in layout request");
                                                                     }
                                                                     flay.invalidate();
                                                     }
                                                     if (mediaplaypause != null)
                                                     {
                                                                     mediaplaypause.bringToFront();
                                                                     mediaplaypause.setEnabled(true);
                                                                     mediaplaypause.setVisibility(View.VISIBLE);
                                                                     try
                                                                     {
                                                                                     flay.requestLayout();
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Exception in layout request");
                                                                     }
                                                                     flay.invalidate();
                                                     }
                                                     if (mediatakepicture != null)
                                                     {
                                                                     mediatakepicture.bringToFront();
                                                                     mediatakepicture.setEnabled(true);
                                                                     mediatakepicture.setVisibility(View.VISIBLE);
                                                                     try
                                                                     {
                                                                                     flay.requestLayout();
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     Log.e(TAG, "Exception in layout request");
                                                                     }
                                                                     flay.invalidate();
                                                     }

                                     }
                     });
     }

     public void showtimeslider()
     {
                     if (sliderstatus)
                     {
                                     return;
                     }
                     ((Activity) this.context).runOnUiThread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     if (guiseektime != null)
                                                     {
                                                                     Log.i(TAG, "Showing slider");
                                                                     sliderstatus = true;
                                                     }
                                     }
                     });

     }

     public void hidetimeslider()
     {
                     if (!sliderstatus)
                     {
                                     return;
                     }
                     ((Activity) this.context).runOnUiThread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     if (guiseektime != null)
                                                     {
                                                                     Log.i(TAG, "Hiding slider");
                                                                     sliderstatus = false;
                                                     }
                                     }
                     });
     }

     public void setnamecolor()
     {
                     if (cnotsetyet)
                     {
                                     ((Activity) this.context).runOnUiThread(new Runnable()
                                     {
                                                     @Override
                                                     public void
run()
                                                     {
                                                                     try
                                                                     {
                                                                                     if (currentlevel.ateami != -1)
                                                                                     {
                                                                                                     final int atm = currentlevel.ateami;
                                                                                                     if (attcallsign != null)
                                                                                                     {
                                                                                                                     if (atm == 1)
                                                                                                                     {
                                                                                                                                     attcallsign.setTextColor(Color.RED);
                                                                                                                     }
                                                                                                                     if (atm == 2)
                                                                                                                     {
                                                                                                                                     attcallsign.setTextColor(Color.CYAN);
                                                                                                                     }
                                                                                                                     if (atm == 3)
                                                                                                                     {
                                                                                                                                     attcallsign.setTextColor(Color.YELLOW);
                                                                                                                     }
                                                                                                                     if (atm == 4)
                                                                                                                     {
                                                                                                                                     attcallsign.setTextColor(Color.GREEN);
                                                                                                                     }
                                                                                                                     if (atm == 5)
                                                                                                                     {
                                                                                                                                     attcallsign.setTextColor(Color.MAGENTA);
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     if (currentlevel.dteami != -1)
                                                                                     {
                                                                                                     final int dtm = currentlevel.dteami;
                                                                                                     if (defcallsign != null)
                                                                                                     {
                                                                                                                     if (dtm == 1)
                                                                                                                     {
                                                                                                                                     defcallsign.setTextColor(Color.RED);
                                                                                                                     }
                                                                                                                     if (dtm == 2)
                                                                                                                     {
                                                                                                                                     defcallsign.setTextColor(Color.CYAN);
                                                                                                                     }
                                                                                                                     if (dtm == 3)
                                                                                                                     {
                                                                                                                                     defcallsign.setTextColor(Color.YELLOW);
                                                                                                                     }
                                                                                                                     if (dtm == 4)
                                                                                                                     {
                                                                                                                                     defcallsign.setTextColor(Color.GREEN);
                                                                                                                     }
                                                                                                                     if (dtm == 5)
                                                                                                                     {
                                                                                                                                     defcallsign.setTextColor(Color.MAGENTA);
                                                                                                                     }

                                                                                                     }
                                                                                     }
                                                                     }
                                                                     catch (Exception f)
                                                                     {
                                                                                     Log.w(TAG, "warning could not set color of text fields");
                                                                     }
                                                                     cnotsetyet = false;
                                                     }
                                     });
                     }

     }

     public void updatetimeslider()
     {
                     // if(!sliderstatus)
                     // {
                     // return;
                     // }
                     final float nowframe = (float) currentlevel.currentframe;
                     final float totframe = (float) currentlevel.maxframes;

                     final int progress = (int) (8000.0 * nowframe / (totframe + 0.01f));
                     if (doupdateframe)
                     {
                                     return;
                     }

                     ((Activity) this.context).runOnUiThread(new Runnable()
                     {

                                     @Override
                                     public void
run()
                                     {
                                                     if (guiseektime != null)
                                                     {
                                                                     guiseektime.setProgress(progress);
                                                     }
                                     }
                     });

     }

     public void clearBackground()
     {
                     Log.i(TAG, "clear Background called");
                     if (glview != null)
                     {
                                     ((Activity) this.context).runOnUiThread(new Runnable()
                                     {

                                                     @Override
                                                     public void
run()
                                                     {
                                                                     glview.setBackgroundResource(0);
                                                                     if (loadingbar != null)
                                                                     {
                                                                                     loadingbar.setVisibility(View.INVISIBLE);
                                                                     }
                                                                     if (txtloadstatus != null)
                                                                     {
                                                                                     txtloadstatus.setVisibility(View.INVISIBLE);
                                                                     }
                                                     }
                                     });
                     }
     }

     public void initmusic()
     {
                     // get default system volume for app.....

                     SharedPreferences sp = context.getSharedPreferences("com.lloydm.geosword.prefs", Activity.MODE_PRIVATE);
                     maxmusicvolume = sp.getFloat("musicvolume", 0.8f);

                     if (musicplayer != null)
                     {
                                     Log.i(TAG, "Releasing music player");
                                     musicplayer.release();
                                     musicplayer = null;
                     }
                     musicisreadytoplay = false;
                     musicstartedplaying = false;
                     Log.i(TAG, "new media player");
                     musicplayer = new MediaPlayer();
                     musicplayer.reset();
                     Log.i(TAG, "setting stream type and volume music player");
                     musicplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                     if (musicflag)
                     {
                                     musicplayer.setVolume(0.1f, 0f);
                     }
                     else
                     {
                                     musicplayer.setVolume(0, 0);
                     }

                     musicplayer.setOnErrorListener(new MediaPlayer.OnErrorListener()
                     {
                                     @Override
                                     public boolean
onError(MediaPlayer mp, int what, int extra)
                                     {
                                                     Log.e(TAG, "Error with media player:what:" + what + " extra:" + extra);

                                                     return true;
                                     }
                     });
                     checkcustommusic();
                     loadaudiotrack();
     }

     private void checkcustommusic()
     {
                     usecustommusic = false;
                     try
                     {
                                     File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/" + com.lloydm.geosword.common.Config.BATTLEMUSICFOLDER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch (Exception ff)
                                     {
                                                     Log.w(TAG, "error mkdir");
                                     }
                                     if (folder.isDirectory() && folder.canRead())
                                     {
                                                     File[] flist = folder.listFiles();
                                                     if (flist != null)
                                                     {
                                                                     int ss = flist.length;
                                                                     int mcount = 0;
                                                                     for (int i = 0; i < ss; i++)
                                                                     {
                                                                                     File f = flist[i];
                                                                                     if (f.isFile() && f.canRead())
                                                                                     {
                                                                                                     // check if it is a valid audio file.....
                                                                                                     // mp3, ogg, m4a will do.....
                                                                                                     if (f.getAbsolutePath().endsWith("mp3") || f.getAbsolutePath().endsWith("ogg") || f.getAbsolutePath().endsWith("m4a"))
                                                                                                     {
                                                                                                                     mcount++;
                                                                                                     }
                                                                                     }
                                                                     }
                                                                     if (mcount > 0)
                                                                     {
                                                                                     // pick a random number.....between 0 and mcount-1
                                                                                     Random mrand = new Random();
                                                                                     int mindex
= 1 + (mrand.nextInt(mcount));
                                                                                     for (int i = 0; i < ss; i++)
                                                                                     {
                                                                                                     File f = flist[i];
                                                                                                     if (f.isFile() && f.canRead())
                                                                                                     {
                                                                                                                     // check if it is a valid audio file.....
                                                                                                                     // mp3, ogg, m4a will do.....
                                                                                                                     if (f.getAbsolutePath().endsWith("mp3") || f.getAbsolutePath().endsWith("ogg") || f.getAbsolutePath().endsWith("m4a"))
                                                                                                                     {
                                                                                                                                     mindex--;
                                                                                                                                     if (mindex == 0)
                                                                                                                                     {
                                                                                                                                                     custommusicfilename = f.getAbsolutePath();
                                                                                                                                                     usecustommusic = true;
                                                                                                                                                     break;
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                     }

                                                                     }
                                                     }
                                     }

                     }
                     catch (Exception e)
                     {
                                     Log.e(TAG, "error checking custom music");

                     }

     }

     public void loadaudiotrack()
     {
                     if (musicplayer != null)
                     {
                                     // pick a track at random.....
                                     int musictrack = irand.nextInt(3); // only 3 tracks now....pity have to change this each time...
                                     AssetFileDescriptor fd = null;
                                     Log.i(TAG, "attempting to open raw resource track " + musictrack);
                                     if (usecustommusic)
                                     {

                                     }
                                     else
                                     {
                                                     try
                                                     {
                                                                     fd = assets.openFd("music/track" + (1 + musictrack) + ".ogg");
                                                     }
                                                     catch (IOException e)
                                                     {
                                                                     // TODO Auto-generated catch block
                                                                     e.printStackTrace();
                                                     }
                                     }
                                     if (fd != null || usecustommusic == true)
                                     {
                                                     try
                                                     {
                                                                     Log.i(TAG, "setting data source for music");
                                                                     if (!usecustommusic)
                                                                     {
                                                                                     musicplayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
                                                                                     fd.close();
                                                                     }
                                                                     else
                                                                     {
                                                                                     musicplayer.setDataSource(custommusicfilename);
                                                                     }
                                                                     musicplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                                                                     {
                                                                                     @Override
                                                                                     public void
onPrepared(MediaPlayer mp)
                                                                                     {
                                                                                                     musicisreadytoplay = true;
                                                                                     }
                                                                     });
                                                                     musicplayer.setLooping(true);

                                                                     // set the volume...or mute it altogether.....
                                                                     musicplayer.prepareAsync();

                                                     }
                                                     catch (IllegalArgumentException e1)
                                                     {
                                                                     // TODO Auto-generated catch block
                                                                     e1.printStackTrace();
                                                                     Log.e(TAG, "Error opening music file - illegala rgument");
                                                     }
                                                     catch (IllegalStateException e1)
                                                     {
                                                                     // TODO Auto-generated catch block
                                                                     Log.e(TAG, "Error opening music file- illegal state");
                                                                     e1.printStackTrace();
                                                     }
                                                     catch (IOException e1)
                                                     {
                                                                     // TODO Auto-generated catch block
                                                                     Log.e(TAG, "Error opening music file - io exception");
                                                                     e1.printStackTrace();
                                                     }
                                                     try
                                                     {
                                                                     if (fd != null)
                                                                     {
                                                                                     fd.close();
                                                                     }
                                                     }
                                                     catch (IOException e)
                                                     {
                                                                     Log.i(TAG, "Error closing audio file - potentially already closed");
                                                     }
                                     }

                     }
     }

     @SuppressLint("NewApi")
     public boolean init3D(Context context)
     {
                     try
                     {
                                     // may have to put this back!
                                     starttime = System.currentTimeMillis();

                                     this.context = context;
                                     // stime = System.currentTimeMillis();
                                     ((Activity) this.context).setContentView(R.layout.activity_test);
                                     flay = (FrameLayout) ((Activity) this.context).findViewById(R.id.flaygl);
                                     if (flay != null)
                                     {
                                                     Log.d(TAG, "mainview not null setting layout nav stuff");
                                                     flay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                                     }
                                     Config.glTransparencyOffset = 0;
                                     Config.glTransparencyMul = 1f / 255f;

                                     // glview = new GLSurfaceView((((Activity)
                                     // this.context).getApplication()));
                                     glview = new GLSurfaceView((Activity) this.context);
                                     if (flay == null)
                                     {
                                                     Log.e(TAG, "flay = null");
                                     }

                                     glview.setEGLContextClientVersion(2);
                                     renderer = new MyRenderer();
                                     glview.setRenderer(renderer);
                                     flay.addView(glview);

                                     guiseektime = new SeekBar((Activity) this.context);
                                     guiseektime.setEnabled(false);
                                     guiseektime.setMax(8000);
                                     guiseektime.setBackgroundResource(R.drawable.menu_seekbar_selector3);
                                     guiseektime.setProgressDrawable(guiseektime.getBackground());

                                     if (Build.VERSION.SDK_INT >= 16)
                                     {
                                                     guiseektime.getThumb().setAlpha(0);
                                     }
                                     final DisplayMetrics metrics = new DisplayMetrics();
                                     ((Activity) this.context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
                                     final int actualWidth = (int) (metrics.widthPixels);

                                     /*
                                         * if(actualWidth>=1920) { isHD = true; //anything less uses lower res / lower quality effects.... }
                                         *
/

                                     final int actualHeight = (int) (metrics.heightPixels);
                                     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     params.gravity = Gravity.START | Gravity.TOP;
                                     params.leftMargin = (int) (actualWidth * 0.025f);
                                     params.topMargin = (int) (actualHeight * 0.9f);
                                     params.width = (int) (actualWidth * 0.95f);
                                     params.height = (int) (actualHeight * 0.065f);
                                     guiseektime.setLayoutParams(params);
                                     guiseektime.getLayoutParams().width = (int) (actualWidth * 0.95f);
                                     guiseektime.getLayoutParams().height = (int) (actualHeight * 0.065f);

                                     if (viewmode)
                                     {
                                                     guiseektime.setVisibility(View.INVISIBLE);
                                     }

                                     flay.addView(guiseektime);

                                     guiseektime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
                                     {
                                                     @Override
                                                     public void
onStopTrackingTouch(SeekBar seekBar)
                                                     {
                                                                     doupdateframe = false;
                                                                     currentlevel.setframe = false;
                                                                     currentlevel.setframex = -1;

                                                     }

                                                     @Override
                                                     public void onStartTrackingTouch(SeekBar seekBar)
                                                     {
                                                                     doupdateframe = true;
                                                                     if (currentlevel != null)
                                                                     {
                                                                                     currentlevel.setframe = true;
                                                                                     currentlevel.setframex = seekBar.getProgress();
                                                                                     if (!replaymode)
                                                                                     {
                                                                                                     ijustpaused = true;
                                                                                     }
                                                                     }
                                                     }

                                                     @Override
                                                     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                                                     {
                                                                     if (!doupdateframe)
                                                                     {
                                                                                     return;
                                                                     }
                                                                     if (currentlevel != null)
                                                                     {
                                                                                     currentlevel.setframe = true;
                                                                                     currentlevel.setframex = progress;
                                                                                     if (!replaymode)
                                                                                     {
                                                                                                     ijustpaused = true;
                                                                                     }
                                                                     }
                                                     }
                                     })
;

                                     attcallsign = new TextView((Activity) this.context);

                                     attcallsign.setTextColor(Color.WHITE);

                                     attcallsign.setVisibility(View.INVISIBLE);
                                     attcallsign.setText(attackercallsign.toUpperCase(Locale.ENGLISH));

                                     FrameLayout.LayoutParams paramsTT = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     paramsTT.gravity = Gravity.START | Gravity.TOP;
                                     paramsTT.leftMargin = (int) (actualWidth * 0.1f);
                                     paramsTT.topMargin = (int) (actualHeight * 0.17f);
                                     paramsTT.width = LayoutParams.WRAP_CONTENT;
                                     paramsTT.height = LayoutParams.WRAP_CONTENT;
                                     attcallsign.setLayoutParams(paramsTT);

                                     flay.addView(attcallsign);

                                     defcallsign = new TextView((Activity) this.context);

                                     defcallsign.setTextColor(Color.WHITE);

                                     defcallsign.setVisibility(View.INVISIBLE);
                                     defcallsign.setText(defendercallsign.toUpperCase(Locale.ENGLISH));

                                     FrameLayout.LayoutParams paramsTT2 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     paramsTT2.gravity = Gravity.START | Gravity.TOP;
                                     paramsTT2.leftMargin = (int) (actualWidth * 0.6f);
                                     paramsTT2.topMargin = (int) (actualHeight * 0.17f);
                                     paramsTT2.width = LayoutParams.WRAP_CONTENT;
                                     paramsTT2.height = LayoutParams.WRAP_CONTENT;
                                     defcallsign.setLayoutParams(paramsTT2);

                                     flay.addView(defcallsign);

                                     btnhighlight = new Button((Activity) this.context);
                                     btnhighlight.setEnabled(true);
                                     btnhighlight.setActivated(false);
                                     btnhighlight.setVisibility(View.INVISIBLE);
                                     btnhighlight.setBackgroundResource(R.drawable.battle_highlight);
                                     FrameLayout.LayoutParams paramsF3 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     paramsF3.gravity = Gravity.CENTER | Gravity.TOP;
                                     paramsF3.topMargin = (int) (actualHeight * 0.025f);
                                     paramsF3.width = (int) (actualWidth * 0.085f);
                                     paramsF3.height = (int) ((float) (paramsF3.width) * (1f / 0.98f));
                                     btnhighlight.setLayoutParams(paramsF3);
                                     btnhighlight.getLayoutParams().width = (int) (actualWidth * 0.085f);
                                     btnhighlight.getLayoutParams().height = (int) ((float) (paramsF3.width) * (1f / 0.98f));
                                     flay.addView(btnhighlight);
                                     btnhighlight.setOnClickListener(new View.OnClickListener()
                                     {

                                                     @Override
                                                     public void
onClick(View v)
                                                     {
                                                                     highlight = !highlight;
                                                                     if (highlight)
                                                                     {
                                                                                     btnhighlight.post(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     btnhighlight.setActivated(true);

                                                                                                     }
                                                                                     });
                                                                     }
                                                                     else
                                                                     {
                                                                                     btnhighlight.post(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     btnhighlight.setActivated(false);

                                                                                                     }
                                                                                     });
                                                                     }
                                                     }
                                     });

                                     mediaplaypause = new Button((Activity) this.context);
                                     mediaplaypause.setEnabled(true);
                                     mediaplaypause.setActivated(true);
                                     mediaplaypause.setVisibility(View.INVISIBLE);
                                     mediaplaypause.setBackgroundResource(R.drawable.media_button_play_pause);

                                     FrameLayout.LayoutParams paramsF = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     paramsF.gravity = Gravity.START | Gravity.TOP;
                                     paramsF.leftMargin = (int) (actualWidth * 0.02f);
                                     paramsF.topMargin = (int) (actualHeight * 0.025f);
                                     paramsF.width = (int) (actualWidth * 0.065f);
                                     paramsF.height = (int) ((float) (paramsF.width) * (0.91f / 1f));
                                     mediaplaypause.setLayoutParams(paramsF);
                                     mediaplaypause.getLayoutParams().width = (int) (actualWidth * 0.1f);
                                     mediaplaypause.getLayoutParams().height = (int) ((float) (paramsF.width) * (0.91f / 1f));

                                     flay.addView(mediaplaypause);
                                     mediaplaypause.setOnClickListener(new View.OnClickListener()
                                     {

                                                     @Override
                                                     public void
onClick(View v)
                                                     {
                                                                     replaymode = !replaymode;
                                                                     if (replaymode == true)
                                                                     {
                                                                                     mediaplaypause.post(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     mediaplaypause.setActivated(true);
                                                                                                     }
                                                                                     });
                                                                     }
                                                                     else
                                                                     {
                                                                                     ijustpaused = true;
                                                                                     mediaplaypause.post(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     mediaplaypause.setActivated(false);
                                                                                                     }
                                                                                     });
                                                                     }

                                                     }
                                     });

                                     // screengrab button.....
                                     mediatakepicture = new Button((Activity) this.context);
                                     mediatakepicture.setEnabled(true);
                                     mediatakepicture.setActivated(false);
                                     mediatakepicture.setVisibility(View.INVISIBLE);
                                     mediatakepicture.setBackgroundResource(R.drawable.media_button_screenshot);

                                     FrameLayout.LayoutParams paramsG = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     paramsG.gravity = Gravity.START | Gravity.TOP;
                                     paramsG.leftMargin = (int) (actualWidth * 0.895f);
                                     paramsG.topMargin = (int) (actualHeight * 0.025f);
                                     paramsG.width = (int) (actualWidth * 0.065f);
                                     paramsG.height = (int) ((float) (paramsG.width) * (1f / 1f));
                                     mediatakepicture.setLayoutParams(paramsG);
                                     mediatakepicture.getLayoutParams().width = (int) (actualWidth * 0.1f);
                                     mediatakepicture.getLayoutParams().height = (int) ((float) (paramsG.width) * (1f / 1f));

                                     flay.addView(mediatakepicture);
                                     mediatakepicture.setOnClickListener(new View.OnClickListener()
                                     {

                                                     @Override
                                                     public void
onClick(View v)
                                                     {

                                                                     if (!takingshot)
                                                                     {
                                                                                     takeshot = true;
                                                                                     takingshot = true;
                                                                                     // takevideo = true;

                                                                                     mediatakepicture.post(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     mediatakepicture.setActivated(true);
                                                                                                                     // tell the system to take a picture with a little boolean
                                                                                                     }
                                                                                     });
                                                                     }
                                                     }
                                     });

                                     // set up the progress bar....//we need to fix this so it moves.....
                                     loadingbar = new SeekBar(((Activity) this.context));
                                     loadingbar.setEnabled(false);
                                     loadingbar.setMax(10000);
                                     loadingbar.setBackgroundResource(R.drawable.menu_seekbar_selector3);
                                     loadingbar.setProgressDrawable(loadingbar.getBackground());

                                     if (Build.VERSION.SDK_INT >= 16)
                                     {
                                                     loadingbar.getThumb().setAlpha(0);
                                     }

                                     FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     params2.gravity = Gravity.START | Gravity.TOP;
                                     params2.leftMargin = (int) (actualWidth * 0.025f);
                                     params2.topMargin = (int) (actualHeight * 0.9f);
                                     params2.width = (int) (actualWidth * 0.95f);
                                     params2.height = (int) (actualHeight * 0.065f);
                                     loadingbar.setLayoutParams(params2);
                                     loadingbar.getLayoutParams().width = (int) (actualWidth * 0.95f);
                                     loadingbar.getLayoutParams().height = (int) (actualHeight * 0.065f);

                                     if (viewmode)
                                     {
                                                     loadingbar.setVisibility(View.INVISIBLE);
                                     }
                                     flay.addView(loadingbar);

                                     // set up the loading status text description for now.....
                                     txtloadstatus = new TextView(((Activity) this.context));
                                     txtloadstatus.setText("Loading...");
                                     txtloadstatus.setTextSize(18f);
                                     txtloadstatus.setTextColor(Color.WHITE);
                                     FrameLayout.LayoutParams params3 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     params3.gravity = Gravity.CENTER;
                                     txtloadstatus.setLayoutParams(params3);
                                     txtloadstatus.setVisibility(View.VISIBLE);
                                     txtloadstatus.setGravity(Gravity.CENTER);
                                     flay.addView(txtloadstatus);

                                     // Log.i(TAG, "TIME:" + System.currentTimeMillis());

                                     long elapsedtime = System.currentTimeMillis() - starttime;

                                     if (elapsedtime > 1000)
                                     {
                                                     Log.i(TAG, "Time taken to init3D:" + (elapsedtime / 1000) + "s.");
                                     }
                                     else
                                     {
                                                     Log.i(TAG, "Time taken to init3D:less than 1 second.");
                                     }
                                     return true;
                     }
                     catch (Exception e)
                     {
                                     for (int i = 0; i < e.getStackTrace().length; i++)
                                     {
                                                     Log.e(TAG, e.getStackTrace()[i].getClassName() + " " + e.getStackTrace()[i].getMethodName() + " " + e.getStackTrace()[i].getLineNumber());

                                     }
                                     Log.e(TAG, "Unable to initialise 3D environment");

                     }
                     return false;
     }

     public void Pause()
     {
                     try
                     {
                                     glview.onPause();
                                     fbuffer.dispose();
                                     world.dispose();
                                     skyboxworld.dispose();
                                     uiworld.dispose();
                                     TextureManager.getInstance().flush();
                                     currentlevel = null;
                     }
                     catch (Exception e)
                     {

                     }
     }

     public void Resume()
     {
                     glview.onResume();
     }

     public boolean isFullScreenOpaque()
     {
                     return true;
     }

     class MyRenderer implements GLSurfaceView.Renderer
     {

                     public long updatetime = 0;

                     public MyRenderer()
                     {
                     }

                     @Override
                     public void onSurfaceCreated(GL10 gl, EGLConfig config)
                     {
                                     // setup shaders....
                                     Resources res = context.getResources();

                                     long elapsedtime = System.currentTimeMillis();

                                     Log.i(TAG, "TIME:(READING SHADERS):" + System.currentTimeMillis());
                                     shaderbasic = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasic)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasic)));
                                     shaderlighting = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_laser)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_laser)));
                                     shaderlightingbump = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_cbump2)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_diffspecbump2)));
                                     shaderbasicwcolor = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasicwcolor)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasicwcolor)));
                                     shaderlightingbumpwcolor = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_cbumpwcolor)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_diffspecbumpwcolor)));
                                     shadercorona = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_corona)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_corona)));
                                     // not just basic...actually has lighting.....
                                     shaderbasiccoloronly = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasiccoloronly)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasiccoloronly)));

                                     shaderbeambisect = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_cbumpbisect)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_diffspecbumpbisect)));

                                     // shaderspriteuv = new
                                     // GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_sprite)),
                                     // Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_sprite)));
                                     shaders = new GLSLShader[8];
                                     shaders[0] = shaderbasic; // standard
                                     shaders[1] = shaderlighting; // used with lasers...
                                     shaders[2] = shaderlightingbump; // //special added a uniform now...called hudstat....slight difference however
                                     shaders[3] = shaderbasicwcolor; // nebula
                                     shaders[4] = shaderlightingbumpwcolor; // planet
                                     shaders[5] = shadercorona; // corona
                                     shaders[6] = shaderbasiccoloronly; // atmosphere
                                     shaders[7] = shaderbeambisect;

                                     // Log.i(TAG, "TIME:(SHADERS READ):" + System.currentTimeMillis());
                                     elapsedtime = System.currentTimeMillis() - elapsedtime;
                                     if (elapsedtime > 1000)
                                     {
                                                     Log.i(TAG, "Time taken to load shaders:" + (elapsedtime / 1000) + "s.");
                                     }
                                     else
                                     {
                                                     Log.i(TAG, "Time taken to load shaders:less than 1 second.");
                                     }

                     }

                     @Override
                     public void onSurfaceChanged(GL10 gl, int width, int height)
                     {
                                     if (fbuffer != null)
                                     {
                                                     fbuffer.dispose();
                                     }
                                     fbuffer = new FrameBuffer(width, height);
                                     if (currentlevel != null)
                                     {
                                                     currentlevel.fbuffer = fbuffer;
                                     }
                                     // only do this if the world doesn't exist yet - I think this should fix the LG issue..good thing I bought the device.

                                     if (world == null)
                                     {
                                                     world = new World();
                                                     world.setClippingPlanes(10, 10000);
                                                     skyboxworld = new World();
                                                     skyboxworld.setClippingPlanes(5, 5000);
                                                     uiworld = new World();
                                                     uiworld.setClippingPlanes(0.1f, 100);
                                                     MemoryHelper.compact();
                                     }
                     }

                     @Override
                     public void
onDrawFrame(GL10 gl)
                     {
                                     if (!startedreplay)
                                     {
                                                     try
                                                     {
                                                                     setLoadingBar(loadingprogress);
                                                                     if (currentlevel != null)
                                                                     {
                                                                                     if (currentlevel.levelloaded)
                                                                                     {
                                                                                                     loadingprogress += (int) ((10000.0f - (float) loadingprogress) * 0.0085f);
                                                                                                     setLoadingStatus(1 + currentlevel.lprog);
                                                                                                     setnamecolor();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     loadingprogress += (int) ((9800.0f - (float) loadingprogress) * 0.0085f);
                                                                                                     setLoadingStatus(0);
                                                                                     }
                                                                     }
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error setting loading bar");

                                                     }
                                     }
                                     if (currentlevel == null && world != null && fbuffer != null)
                                     {
                                                     timetoready = System.currentTimeMillis();
                                                     createskybox();
                                                     skyboxcam = skyboxworld.getCamera();
                                                     skyboxcam.setFOV(1.5f); // play with this....
                                                     currentlevel = new Level3(1, world, assets, shaders, replayfile, viewmode, musicflag);
                                     }
                                     if (updatetime == 0)
                                     {
                                                     updatetime = System.currentTimeMillis();
                                     }

                                     if (camera == null && world != null)
                                     {
                                                     camera = world.getCamera();

                                     }
                                     if (currentlevel != null)
                                     {
                                                     if (currentlevel.safetoexit)
                                                     {
                                                                     // jump out.....exit early..etc
                                                                     // ((Activity)J3D.this.context).finish();
                                                                     // simply return....

                                                                     if (currentlevel.failexit) // if the file load was really bad....which can happen...sometimes....
                                                                     {
                                                                                     Log.w(TAG, "Fail Exit signalled");
                                                                                     try
                                                                                     {
                                                                                                     ((Activity) J3D.this.context).finish();
                                                                                     }
                                                                                     catch (Exception e)
                                                                                     {
                                                                                                     Log.e(TAG, "fail exit failed to finish");
                                                                                     }
                                                                     }

                                                                     return;
                                                     }
                                                     if (currentlevel.levelloaded)
                                                     {
                                                                     if (currentlevel.world == null)
                                                                     {
                                                                                     currentlevel.world = world;
                                                                     }
                                                                     if (currentlevel.fbuffer == null)
                                                                     {
                                                                                     currentlevel.fbuffer = fbuffer;
                                                                     }
                                                                     if (currentlevel.camera == null)
                                                                     {
                                                                                     currentlevel.camera = camera;
                                                                     }
                                                                     if (firstupdate)
                                                                     {
                                                                                     // Log.i(TAG, "TIME:(FIRST UPDATE CLEARING):" + System.currentTimeMillis());
                                                                                     Loader.clearCache();
                                                                                     MemoryHelper.compact();
                                                                                     // Log.i(TAG, "TIME:(FIRST UPDATE CLEARED?):" + System.currentTimeMillis());
                                                                     }
                                                                     if (Math.abs(System.currentTimeMillis() - updatetime) > 15 || firstupdate == true)
                                                                     {
                                                                                     frametime = Math.abs(System.currentTimeMillis() - updatetime);
                                                                                     // skip up 3 frames depending on how slow......
                                                                                     if (!firstupdate)
                                                                                     {
                                                                                                     // Each frame is 16ms (60Hz seems to be the standard update rate)
                                                                                                     // If the frametime goes over 16ms by a multiple of 16 then we should
                                                                                                     // increase the speed....
                                                                                                     // This should work on low spec machines quite well hopefully....

                                                                                                     skipnum = (int) ((frametime - 16) / 16); // was divided by 50 before...
                                                                                                     if (skipnum > 4)
                                                                                                     {
                                                                                                                     skipnum = 4;
                                                                                                     }
                                                                                                     if (skipnum < 0)
                                                                                                     {
                                                                                                                     skipnum = 0;
                                                                                                     }
                                                                                                     currentlevel.skipnum = skipnum;
                                                                                     }
                                                                                     updatetime = System.currentTimeMillis();
                                                                                     if (firstupdate)
                                                                                     {
                                                                                                     firstupdate = false;
                                                                                     }

                                                                                     if (startedreplay == true && replaymode == false && ijustpaused == false)
                                                                                     {
                                                                                                     // do nothing....
                                                                                                     // except move the camera if the user wishes to........
                                                                                                     freelook();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     currentlevel.update(); // do no updates at all if replay mode == false and the game has started...ie

                                                                                                     // we've pressed pause....
                                                                                                     if (replaymode == false)
                                                                                                     {
                                                                                                                     ijustpaused = false;
                                                                                                     }
                                                                                     }
                                                                                     if (currentlevel.currentframe >= 10 && startedreplay == false)
                                                                                     {
                                                                                                     clearBackground();
                                                                                                     startedreplay = true;

                                                                                                     // if(sliderstatus==true){sliderstatus = false;}
                                                                                                     enabledHUD();
                                                                                                     long elapsedtime = System.currentTimeMillis() - timetoready;
                                                                                                     if (elapsedtime > 1000)
                                                                                                     {
                                                                                                                     Log.i(TAG, "Time taken to prepare 3d scene and be ready:" + (elapsedtime / 1000) + "s.");
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     Log.i(TAG, "Time taken to prepare 3d scene and be ready:less than 1 second.");
                                                                                                     }

                                                                                     }
                                                                                     if (startedreplay)
                                                                                     {
                                                                                                     updatetimeslider();
                                                                                                     if (musicisreadytoplay == true && musicstartedplaying == false)
                                                                                                     {
                                                                                                                     musicstartedplaying = true;
                                                                                                                     if (musicplayer != null)
                                                                                                                     {
                                                                                                                                     try
                                                                                                                                     {
                                                                                                                                                     musicplayer.start();
                                                                                                                                     }
                                                                                                                                     catch (Exception e)
                                                                                                                                     {
                                                                                                                                                     Log.e(TAG, "error starting audio playback");

                                                                                                                                     }

                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     if (musicstartedplaying)
                                                                                     {
                                                                                                     if (musicplayer != null)
                                                                                                     {
                                                                                                                     try
                                                                                                                     {
                                                                                                                                     if (musicplayer.isPlaying())
                                                                                                                                     {
                                                                                                                                                     if (replaymode == true)
                                                                                                                                                     {
                                                                                                                                                                     if (currentlevel != null)
                                                                                                                                                                     {
                                                                                                                                                                                     if (currentlevel.currentframe < 1000 && currentlevel.currentframe > 400)
                                                                                                                                                                                     {
                                                                                                                                                                                                     // ramp up the volume of the
                                                                                                                                                                                                     // classical music to
                                                                                                                                                                                                     // maxmusicvolume (0.8 by
                                                                                                                                                                                                     // default)
                                                                                                                                                                                                     // linearly from frame n to
                                                                                                                                                                                                     // 300....
                                                                                                                                                                                                     if (musicflag)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     musicplayer.setVolume(0.1f + (maxmusicvolume - 0.1f) * (float) (currentlevel.currentframe - 400) / 600.0f, 0);
                                                                                                                                                                                                     }
                                                                                                                                                                                                     else
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     musicplayer.setVolume(0, 0);
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                                     else
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (currentlevel.currentframe > currentlevel.maxframes - 300)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (musicflag)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     musicplayer.setVolume(0.1f + (maxmusicvolume - 0.1f) * (float) (currentlevel.maxframes - currentlevel.currentframe) / 300.0f, 0);
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     else
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     musicplayer.setVolume(0, 0);
                                                                                                                                                                                                                     }

                                                                                                                                                                                                     }
                                                                                                                                                                                                     else
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (currentlevel.currentframe <= 400)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (musicflag)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     musicplayer.setVolume(0.1f,0 );
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     musicplayer.setVolume(0, 0);
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     else
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (musicflag)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     musicplayer.setVolume(maxmusicvolume, 0);
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     musicplayer.setVolume(0, 0);
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     // we must have hit pause...
                                                                                                                                                                     // try and pause the track...
                                                                                                                                                                     try
                                                                                                                                                                     {
                                                                                                                                                                                     musicplayer.pause();
                                                                                                                                                                     }
                                                                                                                                                                     catch (Exception ff)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "Error pausing track");
                                                                                                                                                                     }
                                                                                                                                                     }

                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     if (replaymode == true)
                                                                                                                                                     {
                                                                                                                                                                     // we want to play...we are possibly paused.....
                                                                                                                                                                     try
                                                                                                                                                                     {
                                                                                                                                                                                     musicplayer.start();
                                                                                                                                                                     }
                                                                                                                                                                     catch (Exception ffg)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "Error restarting track");
                                                                                                                                                                     }

                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     // don't do anything...we are paused and that's how we should stay....
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     catch (Exception e)
                                                                                                                     {
                                                                                                                                     Log.e(TAG, "Music player most likely in wrong state.");
                                                                                                                     }
                                                                                                     }
                                                                                     }

                                                                                     if (skyboxcam != null && camera != null)
                                                                                     {
                                                                                                     if (v1 == null)
                                                                                                     {
                                                                                                                     v1 = camera.getDirection();
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     v1 = camera.getDirection(v1);
                                                                                                     }
                                                                                                     v2 = camera.getUpVector();
                                                                                                     skyboxcam.setOrientation(v1, v2);
                                                                                                     world.setAmbientLight(255, 255, 255);
                                                                                     }
                                                                                     dodraw = true;
                                                                     }

                                                     }
                                     }
                                     if (firstupdate == false || dodraw == true)
                                     {
                                                     // put the highlight through when necessary......
                                                     if (currentlevel != null)
                                                     {
                                                                     if (highlight)
                                                                     {
                                                                                     currentlevel.showstats = true;
                                                                     }
                                                                     else
                                                                     {
                                                                                     currentlevel.showstats = false;
                                                                     }
                                                     }
                                                     fbuffer.clear();
                                                     if (skyboxworld != null)
                                                     {
                                                                     skyboxworld.renderScene(fbuffer);
                                                                     skyboxworld.draw(fbuffer);
                                                     }
                                                     if (viewmode)
                                                     {
                                                                     fbuffer.clear();
                                                     }
                                                     else
                                                     {
                                                                     fbuffer.clearZBufferOnly();
                                                     }
                                                     world.renderScene(fbuffer);
                                                     world.draw(fbuffer);
                                                     fbuffer.clearZBufferOnly();
                                                     if (uiworld != null)
                                                     {
                                                                     // put the hud stuff on.....if it is ready to....
                                                                     if (currentlevel != null)
                                                                     {
                                                                                     if (currentlevel.levelloaded)
                                                                                     {
                                                                                                     if (uicam == null)
                                                                                                     {
                                                                                                                     uicam = uiworld.getCamera();
                                                                                                                     uiworld.setAmbientLight(255, 255, 255);
                                                                                                                     uicam.setPosition(0, 0, 0);
                                                                                                                     uicam.lookAt(new SimpleVector(0, 0, 1)); // look
                                                                                                                                                                                                                                                                                         // straight
                                                                                                                                                                                                                                                                                         // ahead....
                                                                                                                     currentlevel.createteamhptex(uiworld);
                                                                                                     }
                                                                                     }
                                                                     }

                                                                     uiworld.renderScene(fbuffer);
                                                                     uiworld.draw(fbuffer);
                                                     }

                                                     fbuffer.display();

                                                     // if we are in video mode then capture video (only for me to use really as damn slow)

                                                     // if we are in view mode then capture the screen every 'n'
                                                     // frames - if we are in devscreenshot mode too
                                                     if (takeshot == true && takevideo == false)
                                                     {
                                                                     takeshot = false;

                                                                     // try and take a screenshot and save to the user's device....in the appropriate place.....
                                                                     // in this case in their convergence folder....
                                                                     if (currentlevel != null)
                                                                     {
                                                                                     final int cframe = currentlevel.currentframe;
                                                                                     if (cframe > 2 && cframe < currentlevel.maxframes - 1)
                                                                                     {
                                                                                                     // take a screen grab.....
                                                                                                     try
                                                                                                     {
                                                                                                                     Log.i(TAG, "Taking screenshot");

                                                                                                                     if (glpixels == null)
                                                                                                                     {
                                                                                                                                     glpixels = fbuffer.getPixels();
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     glpixels = fbuffer.getPixels(glpixels);
                                                                                                                     }

                                                                                                                     final int[] glpixels2 = glpixels;

                                                                                                                     new Thread(new Runnable()
                                                                                                                     {

                                                                                                                                     @Override
                                                                                                                                     public void
run()
                                                                                                                                     {
                                                                                                                                                     try
                                                                                                                                                     {
                                                                                                                                                                     // take glpixels2 and reorder it....
                                                                                                                                                                     // we are going to shrink the image by a factor of 2.....
                                                                                                                                                                     // speed things up a little...
                                                                                                                                                                     int[] pix = new int[glpixels2.length];
                                                                                                                                                                     int zzz = 0;
                                                                                                                                                                     int psize = pix.length;
                                                                                                                                                                     int alpha = 255 << 24;
                                                                                                                                                                     int red = 16777215 ^ 65535;
                                                                                                                                                                     int green = 65535 ^ 255;
                                                                                                                                                                     int blue = 255;
                                                                                                                                                                     for (zzz = 0; zzz < psize; zzz++)
                                                                                                                                                                     {
                                                                                                                                                                                     pix[zzz] = alpha | (glpixels2[zzz] & red) >> 16 | (glpixels2[zzz] & green) | (glpixels2[zzz] & blue) << 16;
                                                                                                                                                                     }
                                                                                                                                                                     Bitmap bmp1 = Bitmap.createBitmap(pix, fbuffer.getWidth(), fbuffer.getHeight(), Bitmap.Config.ARGB_8888);
                                                                                                                                                                     FileOutputStream fos = null;
                                                                                                                                                                     BufferedOutputStream bos = null;
                                                                                                                                                                     // File picfolder = new File(com.lloydm.geosword.common.Config.getStorageFolder(context) + com.lloydm.geosword.common.Config.SCREENSHOTFOLDER);
                                                                                                                                                                     File picfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + com.lloydm.geosword.common.Config.SCREENSHOTFOLDER);

                                                                                                                                                                     if (!picfolder.exists())
                                                                                                                                                                     {
                                                                                                                                                                                     picfolder.mkdirs();
                                                                                                                                                                     }
                                                                                                                                                                     if (picfolder.isDirectory())
                                                                                                                                                                     {
                                                                                                                                                                                     // write to it.....
                                                                                                                                                                                     File picfile = new File(picfolder, "img" + System.currentTimeMillis() + "_" + cframe + ".png");
                                                                                                                                                                                     try
                                                                                                                                                                                     {
                                                                                                                                                                                                     fos = new FileOutputStream(picfile);
                                                                                                                                                                                                     bmp1.compress(CompressFormat.PNG, 100, fos);
                                                                                                                                                                                                     bos = new BufferedOutputStream(fos, 8196);
                                                                                                                                                                                                     bos.flush();
                                                                                                                                                                                     }
                                                                                                                                                                                     catch (Exception ef)
                                                                                                                                                                                     {
                                                                                                                                                                                                     Log.e(TAG, "Error opening file for writing");
                                                                                                                                                                                     }
                                                                                                                                                                                     finally
                                                                                                                                                                                     {
                                                                                                                                                                                                     try
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (fos != null)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     fos.close();
                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                                     catch (Exception gg)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     Log.e(TAG, "Error with pic fos");
                                                                                                                                                                                                     }
                                                                                                                                                                                                     try
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (bos != null)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     bos.close();
                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                                     catch (Exception gg)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     Log.e(TAG, "Error with pic bos");
                                                                                                                                                                                                     }
                                                                                                                                                                                                     try
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     bmp1.recycle();
                                                                                                                                                                                                     }
                                                                                                                                                                                                     catch (Exception gg)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     Log.e(TAG, "Error recycling bitmap...");
                                                                                                                                                                                                     }

                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     catch (Exception ffg2)
                                                                                                                                                     {
                                                                                                                                                                     // something went hideously wrong...hideously!
                                                                                                                                                                     Log.e(TAG, "Something's wrong, something's amiss - I fear something terrible has happened...like the sound of a million voices crying out in terror and then were suddenly silenced....");
                                                                                                                                                     }
                                                                                                                                                     takingshot = false;
                                                                                                                                                     if (mediatakepicture != null)
                                                                                                                                                     {
                                                                                                                                                                     mediatakepicture.post(new Runnable()
                                                                                                                                                                     {
                                                                                                                                                                                     @Override
                                                                                                                                                                                     public void
run()
                                                                                                                                                                                     {
                                                                                                                                                                                                     mediatakepicture.setActivated(false);
                                                                                                                                                                                                     // tell the system to take a picture with a little boolean
                                                                                                                                                                                     }
                                                                                                                                                                     });
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }).start();
                                                                                                     }
                                                                                                     catch (Exception e)
                                                                                                     {
                                                                                                                     Log.e(TAG, "Error with screen grab");
                                                                                                     }
                                                                                     }
                                                                     }

                                                     }
                                     }
                     }
     }

     private void initplanet()
     {
                     Texture ptex = null;
                     InputStream intex1 = null;
                     try
                     {
                                     int ppi = 1 + irand.nextInt(5);
                                     if (attplanetid > 0 && attplanetid < 6)
                                     {
                                                     ppi = attplanetid;
                                     }
                                     intex1 = assets.open("environment/planet" + ppi + ".png");

                                     ptex = new Texture(intex1, true);
                                     TextureManager.getInstance().addTexture("ptex", ptex);
                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for galaxy");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }
                     skybox[6] = ExtendedPrimitives.createSprite(30f, 30f);
                     skybox[6].setBillboarding(true);// .createSphere(2.0f,32);
                     skybox[6].setTexture("ptex");

                     // turn off planet for now......
                     skybox[6].setTransparencyMode(Object3D.TRANSPARENCY_MODE_DEFAULT);
                     skybox[6].setTransparency(254);
                     skybox[6].setShader(shaderbasic);
                     skybox[6].build();
                     skybox[6].strip();
                     skybox[6].setVisibility(true);
                     skyboxworld.addObject(skybox[6]);

                     skybox[6].translate(14f, 13f, -55f);
     }

     private void initnebulacube()
     {

                     InputStream intex1 = null;

                     Texture fr = null;
                     Texture bk = null;
                     Texture lf = null;
                     Texture rt = null;
                     Texture up = null;
                     Texture dn = null;

                     Texture fr2 = null;
                     Texture bk2 = null;
                     Texture lf2 = null;
                     Texture rt2 = null;
                     Texture up2 = null;
                     Texture dn2 = null;

                     int nc = 1 + irand.nextInt(3);
                     int nc2 = 1 + irand.nextInt(3);
                     if (nc2 == nc)
                     {
                                     nc2++;
                                     if (nc2 > 3)
                                     {
                                                     nc2 = 1;
                                     }
                     }

                     String sfile = "/skybox";
                     if (!isHD)
                     {
                                     sfile = "/skybox";
                     }
                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + sfile + "bk.jpg");

                                     bk = new Texture(intex1, false);
                                     TextureManager.getInstance().addTexture("skybk", bk);

                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for bk");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }

                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + sfile + "dn.jpg");

                                     dn = new Texture(intex1, false);
                                     TextureManager.getInstance().addTexture("skydn", dn);

                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for dn");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }

                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + sfile + "fr.jpg");

                                     fr = new Texture(intex1, false);
                                     TextureManager.getInstance().addTexture("skyfr", fr);

                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for fr");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }

                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + sfile + "lf.jpg");

                                     lf = new Texture(intex1, false);
                                     TextureManager.getInstance().addTexture("skylf", lf);

                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for lf");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }

                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + sfile + "rt.jpg");

                                     rt = new Texture(intex1, false);
                                     TextureManager.getInstance().addTexture("skyrt", rt);

                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for rt");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }

                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + sfile + "up.jpg");

                                     up = new Texture(intex1, false);
                                     TextureManager.getInstance().addTexture("skyup", up);

                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "Error opening texture for up");
                                     e.printStackTrace();
                     }
                     finally
                     {
                                     try
                                     {
                                                     intex1.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing intex1");
                                     }
                     }

                     // 2nd texture.....
                     if (!com.lloydm.geosword.common.Config.ISLOWSPECDEVICE)
                     {
                                     try
                                     {
                                                     intex1 = assets.open("environment/box" + nc2 + sfile + "bk.jpg");

                                                     bk2 = new Texture(intex1, false);
                                                     TextureManager.getInstance().addTexture("skybk2", bk2);

                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "Error opening texture for bk2");
                                                     e.printStackTrace();
                                     }
                                     finally
                                     {
                                                     try
                                                     {
                                                                     intex1.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing intex1");
                                                     }
                                     }

                                     try
                                     {
                                                     intex1 = assets.open("environment/box" + nc2 + sfile + "dn.jpg");

                                                     dn2 = new Texture(intex1, false);
                                                     TextureManager.getInstance().addTexture("skydn2", dn2);

                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "Error opening texture for dn2");
                                                     e.printStackTrace();
                                     }
                                     finally
                                     {
                                                     try
                                                     {
                                                                     intex1.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing intex1");
                                                     }
                                     }

                                     try
                                     {
                                                     intex1 = assets.open("environment/box" + nc2 + sfile + "fr.jpg");

                                                     fr2 = new Texture(intex1, false);
                                                     TextureManager.getInstance().addTexture("skyfr2", fr2);

                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "Error opening texture for fr2");
                                                     e.printStackTrace();
                                     }
                                     finally
                                     {
                                                     try
                                                     {
                                                                     intex1.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing intex1");
                                                     }
                                     }

                                     try
                                     {
                                                     intex1 = assets.open("environment/box" + nc2 + sfile + "lf.jpg");

                                                     lf2 = new Texture(intex1, false);
                                                     TextureManager.getInstance().addTexture("skylf2", lf2);

                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "Error opening texture for lf2");
                                                     e.printStackTrace();
                                     }
                                     finally
                                     {
                                                     try
                                                     {
                                                                     intex1.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing intex1");
                                                     }
                                     }

                                     try
                                     {
                                                     intex1 = assets.open("environment/box" + nc2 + sfile + "rt.jpg");

                                                     rt2 = new Texture(intex1, false);
                                                     TextureManager.getInstance().addTexture("skyrt2", rt2);

                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "Error opening texture for rt2");
                                                     e.printStackTrace();
                                     }
                                     finally
                                     {
                                                     try
                                                     {
                                                                     intex1.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing intex1");
                                                     }
                                     }

                                     try
                                     {
                                                     intex1 = assets.open("environment/box" + nc2 + sfile + "up.jpg");

                                                     up2 = new Texture(intex1, false);
                                                     TextureManager.getInstance().addTexture("skyup2", up2);

                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "Error opening texture for up2");
                                                     e.printStackTrace();
                                     }
                                     finally
                                     {
                                                     try
                                                     {
                                                                     intex1.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing intex1");
                                                     }
                                     }
                     }
                     // first cube....
                     // front
                     float ds = 600f;
                     skybox[7] = new Object3D(2);
                     skybox[7].setCulling(false);
                     skybox[7].addTriangle(new SimpleVector(-1 * ds, -1 * ds, 1f * ds), 0, 1, new SimpleVector(1 * ds, -1 * ds, 1f * ds), 1, 1, new SimpleVector(1 * ds, 1 * ds, 1f * ds), 1, 0);
                     skybox[7].addTriangle(new SimpleVector(1 * ds, 1 * ds, 1f * ds), 1, 0, new SimpleVector(-1 * ds, 1 * ds, 1f * ds), 0, 0, new SimpleVector(-1 * ds, -1 * ds, 1f * ds), 0, 1);
                     skybox[7].setTexture("skyfr");
                     skybox[7].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                     skybox[7].setTransparency(254);
                     skybox[7].setShader(shaderbasic);
                     skybox[7].build();
                     skybox[7].strip();
                     skybox[7].setVisibility(true);
                     skyboxworld.addObject(skybox[7]);

                     // left
                     skybox[8] = new Object3D(2);
                     skybox[8].setCulling(false);
                     skybox[8].addTriangle(new SimpleVector(1f * ds, -1 * ds, -1 * ds), 1, 1, new SimpleVector(1f * ds, -1 * ds, 1 * ds), 0, 1, new SimpleVector(1f * ds, 1 * ds, 1 * ds), 0, 0);
                     skybox[8].addTriangle(new SimpleVector(1f * ds, 1 * ds, 1 * ds), 0, 0, new SimpleVector(1f * ds, 1 * ds, -1 * ds), 1, 0, new SimpleVector(1f * ds, -1 * ds, -1 * ds), 1, 1);
                     skybox[8].setTexture("skylf");
                     skybox[8].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                     skybox[8].setTransparency(254);
                     skybox[8].setShader(shaderbasic);
                     skybox[8].build();
                     skybox[8].strip();
                     skybox[8].setVisibility(true);
                     skyboxworld.addObject(skybox[8]);

                     // back
                     skybox[9] = new Object3D(2);
                     skybox[9].setCulling(false);
                     skybox[9].addTriangle(new SimpleVector(-1 * ds, -1 * ds, -1f * ds), 1, 1, new SimpleVector(1 * ds, -1 * ds, -1f * ds), 0, 1, new SimpleVector(1 * ds, 1 * ds, -1f * ds), 0, 0);
                     skybox[9].addTriangle(new SimpleVector(1 * ds, 1 * ds, -1f * ds), 0, 0, new SimpleVector(-1 * ds, 1 * ds, -1f * ds), 1, 0, new SimpleVector(-1 * ds, -1 * ds, -1f * ds), 1, 1);
                     skybox[9].setTexture("skybk");
                     skybox[9].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                     skybox[9].setTransparency(254);
                     skybox[9].setShader(shaderbasic);
                     skybox[9].build();
                     skybox[9].strip();
                     skybox[9].setVisibility(true);
                     skyboxworld.addObject(skybox[9]);

                     // right
                     skybox[10] = new Object3D(2);
                     skybox[10].setCulling(false);
                     skybox[10].addTriangle(new SimpleVector(-1f * ds, -1 * ds, -1 * ds), 0, 1, new SimpleVector(-1f * ds, -1 * ds, 1 * ds), 1, 1, new SimpleVector(-1f * ds, 1 * ds, 1 * ds), 1, 0);
                     skybox[10].addTriangle(new SimpleVector(-1f * ds, 1 * ds, 1 * ds), 1, 0, new SimpleVector(-1f * ds, 1 * ds, -1 * ds), 0, 0, new SimpleVector(-1f * ds, -1 * ds, -1 * ds), 0, 1);
                     skybox[10].setTexture("skyrt");
                     skybox[10].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                     skybox[10].setTransparency(254);
                     skybox[10].setShader(shaderbasic);
                     skybox[10].build();
                     skybox[10].strip();
                     skybox[10].setVisibility(true);
                     skyboxworld.addObject(skybox[10]);

                     // bottom
                     skybox[11] = new Object3D(2);
                     skybox[11].setCulling(false);
                     skybox[11].addTriangle(new SimpleVector(-1 * ds, -1f * ds, -1 * ds), 1, 0, new SimpleVector(1 * ds, -1f * ds, -1 * ds), 0, 0, new SimpleVector(1 * ds, -1f * ds, 1 * ds), 0, 1);
                     skybox[11].addTriangle(new SimpleVector(1 * ds, -1f * ds, 1 * ds), 0, 1, new SimpleVector(-1 * ds, -1f * ds, 1 * ds), 1, 1, new SimpleVector(-1 * ds, -1f * ds, -1 * ds), 1, 0);
                     skybox[11].setTexture("skydn");
                     skybox[11].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                     skybox[11].setTransparency(254);
                     skybox[11].setShader(shaderbasic);
                     skybox[11].build();
                     skybox[11].strip();
                     skybox[11].setVisibility(true);
                     skyboxworld.addObject(skybox[11]);

                     // top
                     skybox[12] = new Object3D(2);
                     skybox[12].setCulling(false);
                     skybox[12].addTriangle(new SimpleVector(-1 * ds, 1f * ds, -1 * ds), 1, 1, new SimpleVector(1 * ds, 1f * ds, -1 * ds), 0, 1, new SimpleVector(1 * ds, 1f * ds, 1 * ds), 0, 0);
                     skybox[12].addTriangle(new SimpleVector(1 * ds, 1f * ds, 1 * ds), 0, 0, new SimpleVector(-1 * ds, 1f * ds, 1 * ds), 1, 0, new SimpleVector(-1 * ds, 1f * ds, -1 * ds), 1, 1);
                     skybox[12].setTexture("skyup");
                     skybox[12].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                     skybox[12].setTransparency(254);
                     skybox[12].setShader(shaderbasic);
                     skybox[12].build();
                     skybox[12].strip();
                     skybox[12].setVisibility(true);
                     skyboxworld.addObject(skybox[12]);

                     if (!com.lloydm.geosword.common.Config.ISLOWSPECDEVICE)
                     {
                                     // 2nd cube...slightly closer in...additive blended as well.
                                     // also has to be rotated in a random manner....
                                     ds = 375f;
                                     skybox[13] = new Object3D(2);
                                     skybox[13].setCulling(false);
                                     skybox[13].addTriangle(new SimpleVector(-1 * ds, -1 * ds, 1f * ds), 0, 1, new SimpleVector(1 * ds, -1 * ds, 1f * ds), 1, 1, new SimpleVector(1 * ds, 1 * ds, 1f * ds), 1, 0);
                                     skybox[13].addTriangle(new SimpleVector(1 * ds, 1 * ds, 1f * ds), 1, 0, new SimpleVector(-1 * ds, 1 * ds, 1f * ds), 0, 0, new SimpleVector(-1 * ds, -1 * ds, 1f * ds), 0, 1);
                                     skybox[13].setTexture("skyfr2");
                                     skybox[13].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                                     skybox[13].setTransparency(254);
                                     skybox[13].setShader(shaderbasic);
                                     skybox[13].build();
                                     skybox[13].strip();
                                     skybox[13].setVisibility(true);
                                     skyboxworld.addObject(skybox[13]);

                                     // left
                                     skybox[14] = new Object3D(2);
                                     skybox[14].setCulling(false);
                                     skybox[14].addTriangle(new SimpleVector(1f * ds, -1 * ds, -1 * ds), 1, 1, new SimpleVector(1f * ds, -1 * ds, 1 * ds), 0, 1, new SimpleVector(1f * ds, 1 * ds, 1 * ds), 0, 0);
                                     skybox[14].addTriangle(new SimpleVector(1f * ds, 1 * ds, 1 * ds), 0, 0, new SimpleVector(1f * ds, 1 * ds, -1 * ds), 1, 0, new SimpleVector(1f * ds, -1 * ds, -1 * ds), 1, 1);
                                     skybox[14].setTexture("skylf2");
                                     skybox[14].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                                     skybox[14].setTransparency(254);
                                     skybox[14].setShader(shaderbasic);
                                     skybox[14].build();
                                     skybox[14].strip();
                                     skybox[14].setVisibility(true);
                                     skyboxworld.addObject(skybox[14]);

                                     // back
                                     skybox[15] = new Object3D(2);
                                     skybox[15].setCulling(false);
                                     skybox[15].addTriangle(new SimpleVector(-1 * ds, -1 * ds, -1f * ds), 1, 1, new SimpleVector(1 * ds, -1 * ds, -1f * ds), 0, 1, new SimpleVector(1 * ds, 1 * ds, -1f * ds), 0, 0);
                                     skybox[15].addTriangle(new SimpleVector(1 * ds, 1 * ds, -1f * ds), 0, 0, new SimpleVector(-1 * ds, 1 * ds, -1f * ds), 1, 0, new SimpleVector(-1 * ds, -1 * ds, -1f * ds), 1, 1);
                                     skybox[15].setTexture("skybk2");
                                     skybox[15].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                                     skybox[15].setTransparency(254);
                                     skybox[15].setShader(shaderbasic);
                                     skybox[15].build();
                                     skybox[15].strip();
                                     skybox[15].setVisibility(true);
                                     skyboxworld.addObject(skybox[15]);

                                     // right
                                     skybox[16] = new Object3D(2);
                                     skybox[16].setCulling(false);
                                     skybox[16].addTriangle(new SimpleVector(-1f * ds, -1 * ds, -1 * ds), 0, 1, new SimpleVector(-1f * ds, -1 * ds, 1 * ds), 1, 1, new SimpleVector(-1f * ds, 1 * ds, 1 * ds), 1, 0);
                                     skybox[16].addTriangle(new SimpleVector(-1f * ds, 1 * ds, 1 * ds), 1, 0, new SimpleVector(-1f * ds, 1 * ds, -1 * ds), 0, 0, new SimpleVector(-1f * ds, -1 * ds, -1 * ds), 0, 1);
                                     skybox[16].setTexture("skyrt2");
                                     skybox[16].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                                     skybox[16].setTransparency(254);
                                     skybox[16].setShader(shaderbasic);
                                     skybox[16].build();
                                     skybox[16].strip();
                                     skybox[16].setVisibility(true);
                                     skyboxworld.addObject(skybox[16]);

                                     // bottom
                                     skybox[17] = new Object3D(2);
                                     skybox[17].setCulling(false);
                                     skybox[17].addTriangle(new SimpleVector(-1 * ds, -1f * ds, -1 * ds), 1, 0, new SimpleVector(1 * ds, -1f * ds, -1 * ds), 0, 0, new SimpleVector(1 * ds, -1f * ds, 1 * ds), 0, 1);
                                     skybox[17].addTriangle(new SimpleVector(1 * ds, -1f * ds, 1 * ds), 0, 1, new SimpleVector(-1 * ds, -1f * ds, 1 * ds), 1, 1, new SimpleVector(-1 * ds, -1f * ds, -1 * ds), 1, 0);
                                     skybox[17].setTexture("skydn2");
                                     skybox[17].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                                     skybox[17].setTransparency(254);
                                     skybox[17].setShader(shaderbasic);
                                     skybox[17].build();
                                     skybox[17].strip();
                                     skybox[17].setVisibility(true);
                                     skyboxworld.addObject(skybox[17]);

                                     // top
                                     skybox[18] = new Object3D(2);
                                     skybox[18].setCulling(false);
                                     skybox[18].addTriangle(new SimpleVector(-1 * ds, 1f * ds, -1 * ds), 1, 1, new SimpleVector(1 * ds, 1f * ds, -1 * ds), 0, 1, new SimpleVector(1 * ds, 1f * ds, 1 * ds), 0, 0);
                                     skybox[18].addTriangle(new SimpleVector(1 * ds, 1f * ds, 1 * ds), 0, 0, new SimpleVector(-1 * ds, 1f * ds, 1 * ds), 1, 0, new SimpleVector(-1 * ds, 1f * ds, -1 * ds), 1, 1);
                                     skybox[18].setTexture("skyup2");
                                     skybox[18].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD);
                                     skybox[18].setTransparency(254);
                                     skybox[18].setShader(shaderbasic);
                                     skybox[18].build();
                                     skybox[18].strip();
                                     skybox[18].setVisibility(true);
                                     skyboxworld.addObject(skybox[18]);

                                     float a1 = irand.nextFloat() * 4f;
                                     float a2 = irand.nextFloat() * 4f;

                                     // rotate objects 13 through 18
                                     Object3D dummy = Object3D.createDummyObj();
                                     for (int kk = 13; kk < 19; kk++)
                                     {
                                                     skybox[kk].addParent(dummy);
                                     }
                                     dummy.rotateX(a1);
                                     dummy.rotateY(a2);
                     }

     }

     /*
         * private void initgalaxy() { InputStream intex2 = null; Texture starmtex = null;
         *
         * try { intex2 = assets.open("environment/stars.png");
         *
         * starmtex = new Texture(intex2, true); TextureManager.getInstance().addTexture("stars", starmtex);
         *
         * } catch (IOException e) { Log.e(TAG, "Error opening texture(2) for galaxy"); e.printStackTrace(); } finally { try { intex2.close(); } catch (Exception e) { Log.e(TAG, "Error closing intex2"); }
         * }
         *
         * if (starmtex != null) {
         *
         * skybox[4] = new Object3D(12); skybox[4].setCulling(false);
         *
         * float ds = 700f; float tmult = 1.5f;
         *
         * skybox[4].addTriangle(new SimpleVector(-1 * ds, -1 * ds, -1 * ds), 0, 0, new SimpleVector(1 * ds, -1 * ds, -1 * ds), tmult, 0, new SimpleVector(1 * ds, -1 * ds, 1 * ds), tmult, tmult);
         * skybox[4].addTriangle(new SimpleVector(1 * ds, -1 * ds, 1 * ds), tmult, tmult, new SimpleVector(-1 * ds, -1 * ds, 1 * ds), 0, tmult, new SimpleVector(-1 * ds, -1 * ds, -1 * ds), 0, 0);
         *
         * skybox[4].addTriangle(new SimpleVector(-1 * ds, 1 * ds, -1 * ds), 0, tmult, new SimpleVector(1 * ds, 1 * ds, -1 * ds), tmult, tmult, new SimpleVector(1 * ds, 1 * ds, 1 * ds), tmult, 0);
         * skybox[4].addTriangle(new SimpleVector(1 * ds, 1 * ds, 1 * ds), tmult, 0, new SimpleVector(-1 * ds, 1 * ds, 1 * ds), 0, 0, new SimpleVector(-1 * ds, 1 * ds, -1 * ds), 0, tmult);
         *
         * skybox[4].addTriangle(new SimpleVector(-1 * ds, -1 * ds, -1 * ds), 0, 0, new SimpleVector(1 * ds, -1 * ds, -1 * ds), tmult, 0, new SimpleVector(1 * ds, 1 * ds, -1 * ds), tmult, tmult);
         * skybox[4].addTriangle(new SimpleVector(1 * ds, 1 * ds, -1 * ds), tmult, tmult, new SimpleVector(-1 * ds, 1 * ds, -1 * ds), 0, tmult, new SimpleVector(-1 * ds, -1 * ds, -1 * ds), 0, 0);
         *
         * skybox[4].addTriangle(new SimpleVector(-1 * ds, -1 * ds, 1 * ds), 0, tmult, new SimpleVector(1 * ds, -1 * ds, 1 * ds), tmult, tmult, new SimpleVector(1 * ds, 1 * ds, 1 * ds), tmult, 0);
         * skybox[4].addTriangle(new SimpleVector(1 * ds, 1 * ds, 1 * ds), tmult, 0, new SimpleVector(-1 * ds, 1 * ds, 1 * ds), 0, 0, new SimpleVector(-1 * ds, -1 * ds, 1 * ds), 0, tmult);
         *
         * skybox[4].addTriangle(new SimpleVector(-1 * ds, -1 * ds, -1 * ds), 0, 0, new SimpleVector(-1 * ds, -1 * ds, 1 * ds), tmult, 0, new SimpleVector(-1 * ds, 1 * ds, 1 * ds), tmult, tmult);
         * skybox[4].addTriangle(new SimpleVector(-1 * ds, 1 * ds, 1 * ds), tmult, tmult, new SimpleVector(-1 * ds, 1 * ds, -1 * ds), 0, tmult, new SimpleVector(-1 * ds, -1 * ds, -1 * ds), 0, 0);
         *
         * skybox[4].addTriangle(new SimpleVector(1 * ds, -1 * ds, -1 * ds), 0, tmult, new SimpleVector(1 * ds, -1 * ds, 1 * ds), tmult, tmult, new SimpleVector(1 * ds, 1 * ds, 1 * ds), tmult, 0);
         * skybox[4].addTriangle(new SimpleVector(1 * ds, 1 * ds, 1 * ds), tmult, 0, new SimpleVector(1 * ds, 1 * ds, -1 * ds), 0, 0, new SimpleVector(1 * ds, -1 * ds, -1 * ds), 0, tmult);
         *
         * skybox[4].setTexture("stars");
         *
         * skybox[4].setShader(shaderbasic); skybox[4].build(); skybox[4].strip(); skybox[4].setVisibility(true); skyboxworld.addObject(skybox[4]);
         *
         * } }
         *
/
     /*
         * private void initsun() { // create the SUN texture InputStream intex1 = null; Texture tex = null;
         *
         * try { intex1 = assets.open("environment/suntex.jpg");
         *
         * tex = new Texture(intex1); TextureManager.getInstance().addTexture("sun", tex);
         *
         * } catch (IOException e) { Log.e(TAG, "Error opening texture for sun"); e.printStackTrace(); } finally { try { intex1.close(); } catch (Exception e) { Log.e(TAG, "Error closing intex1"); } }
         * createsunquad(0, 8.0f, tex, 255, 255, 255, 1.0f);
         *
         * skybox[0].translate(55f, 15f, 65f); }
         *
         * private void createsunquad(int oi, float size, Texture tex, float r, float g, float b, float a) { skybox[oi] = new Object3D(2); skybox[oi].setCulling(false); skybox[oi].addTriangle(new
         * SimpleVector(1f * size, 1f * size, 0), 0, 0, new SimpleVector(-1f * size, 1f * size, 0), 1, 0, new SimpleVector(-1f * size, -1f * size, 0), 1, 1)
; skybox[oi].addTriangle(new SimpleVector(1f *
         * size, 1f * size, 0)
, 0, 0, new SimpleVector(-1f * size, -1f * size, 0), 1, 1, new SimpleVector(1f * size, -1f * size, 0), 0, 1)
; // full bright skybox[oi].setTexture("sun");
         * skybox[oi].setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD); skybox[oi].setTransparency((int) (255f * a));
         *
         * skybox[oi].setShader(shaderbasic);
         *
         * skybox[oi].build(); skybox[oi].strip(); skybox[oi].setVisibility(true); skybox[oi].setBillboarding(true); skyboxworld.addObject(skybox[oi]); Log.i(TAG, "Sun added"); }
         *
/
     private void
createskybox()
     {
                     // implement code from blitz forum for this....
                     // will be very useful.......
                     Log.i(TAG, "TIME:(BEGIN SKYBOX):" + System.currentTimeMillis());
                     if (skybox == null)
                     {
                                     skybox = new Object3D[7 + 6 + 6]; // 1 for the SUN object and '2' for the
                                     // galaxy and '1' for the planet/moon
                                     // empty indices now...specifically 1,2,3
                                     // final 6 indices are now the skybox....which you will use from 3drt images...we may hide the others later..
                                     // try and get it right first time...two skyboxes...overlapping.....
                                     // /stars->skybox->skybox2(rotated and slightly smaller)

                                     // initsun();
                                     // initgalaxy();
                                     initplanet();
                                     initnebulacube();
                     }
                     Log.i(TAG, "TIME:(END SKYBOX):" + System.currentTimeMillis());
     }

     private void freelook()
     {
                     if (Input.xpos != -1 && Input.ypos != -1 && Input.down == true)
                     {
                                     if (Input.xpos < 2.0f * fbuffer.getWidth() / 5)
                                     {
                                                     // rotate in plane forwards/side and retain the same normalized up vector
                                                     //
                                                     uu = camera.getDirection();
                                                     vv = camera.getSideVector();
                                                     ww = camera.getUpVector();

                                                     vv.scalarMul(-0.05f);
                                                     vv.add(uu);
                                                     vv = vv.normalize(vv);

                                                     // vv is in new direction but the up vector may have changed.....
                                                     // that means that ww dotted with vv won't be equal to zero anymore....
                                                     camera.setOrientation(vv, ww);

                                     }
                                     if (Input.xpos > 3.0f * fbuffer.getWidth() / 5)
                                     {
                                                     uu = camera.getDirection();
                                                     vv = camera.getSideVector();
                                                     ww = camera.getUpVector();

                                                     vv.scalarMul(0.05f);
                                                     vv.add(uu);
                                                     vv = vv.normalize(vv);

                                                     // vv is in new direction but the up vector may have changed.....
                                                     // that means that ww dotted with vv won't be equal to zero anymore....
                                                     camera.setOrientation(vv, ww);

                                     }
                                     if(Input.ypos < 2.0f*fbuffer.getHeight()/5)
                                     {
                                             uu = camera.getDirection();
                                             vv = camera.getUpVector();
                                             ww = camera.getDirection();
                                             yy = camera.getUpVector();
                                             ww.scalarMul(-0.05f);
                                             yy.scalarMul(0.05f);
                                             uu.add(yy);
                                             vv.add(ww);
                                             uu = uu.normalize();
                                             vv = vv.normalize();
                                             camera.setOrientation(uu,vv);
                                                                            
                                            
                                     }
                                     if(Input.ypos > 3.0f*fbuffer.getHeight()/5)
                                     {
                                                     uu = camera.getDirection();
                                                     vv = camera.getUpVector();
                                                     ww = camera.getDirection();
                                                     yy = camera.getUpVector();
                                                     ww.scalarMul(0.05f);
                                                     yy.scalarMul(-0.05f);
                                                     uu.add(yy);
                                                     vv.add(ww);
                                                     uu = uu.normalize();
                                                     vv = vv.normalize();
                                                     camera.setOrientation(uu,vv);
                                                    
                                            
                                     }
                                     if (Input.xpos > 2.0f * fbuffer.getWidth() / 5 && Input.xpos < 3.0f * fbuffer.getWidth() / 5 && Input.ypos > 2.0f * fbuffer.getHeight() / 5 && Input.ypos < 3.0f * fbuffer.getHeight() / 5)
                                     {
                                                     // move the camera forwards......
                                                     camera.moveCamera(camera.getDirection(), 2.0f);
                                     }
                     }
     }
}