package com.lloydm.geosword;

import java.io.IOException;
import java.io.InputStream;
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.res.AssetManager;
import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.widget.FrameLayout;

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

     private Random irand = null;

     private int cframe = 0;
     private int meshid = -1;
     private com.lloydm.geosword.Mesh mymesh = null;

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

     private float camdist = 100f;

     private SimpleVector campos = null;
     private SimpleVector v1, v2;

     private World uiworld;

     private boolean issetup = false;

     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[] shaders;

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

     private MyRenderer renderer;

     private final static String TAG = "com.lloydm.geosword.J3DMini";
     private Context context;
     public AssetManager assets;
     public Camera camera;

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

     public J3DMini(AssetManager assets, int meshid)
     {
                     this.meshid = meshid;
                     this.assets = assets;
                     irand = new Random();
                     irand.setSeed(System.currentTimeMillis());
     }

     @SuppressLint("NewApi")

     public boolean init3D(Context context, int aa)
     {
                     try
                     {
                                     // may have to put this back!
                                     this.context = context;
                                     if (aa == 1)
                                     {
                                                     flay = (FrameLayout) ((Activity) this.context).findViewById(R.id.ship3dview);
                                     }
                                     else
                                     {
                                                     // flay = (FrameLayout) ((Activity) this.context)
                                                     // .findViewById(R.id.ship3dview2);

                                     }
                                     Config.glTransparencyOffset = 0;
                                     Config.glTransparencyMul = 1f / 255f;
                                     glview = new GLSurfaceView((Activity) this.context);
                                     if (flay == null)
                                     {
                                                     Log.e(TAG, "flay = null");

                                     }

                                     glview.setEGLContextClientVersion(2);
                                     glview.setZOrderOnTop(false);
                                     renderer = new MyRenderer();
                                     glview.setRenderer(renderer);
                                     glview.setAlpha(0);
                                     flay.addView(glview);

                                     if (aa == 2)
                                     {
                                                     flay.getRootView().invalidate();
                                     }
                                     Log.e(TAG, "3d stuff should actually be working!");
                                     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();
                     }
                     catch (Exception e)
                     {
                                     Log.e(TAG, "Error destroying gl stuff");
                     }
     }

     public void Resume()
     {
                     try
                     {
                                     glview.onResume();
                     }
                     catch (Exception e)
                     {
                                     Log.e(TAG, "Error resuming glview");
                     }
     }

     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();

                                     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_wnormal)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_diffspec)));
                                     shaderlightingbump = new GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_cbump)), Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_diffspecbump)));
                                     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)));
                                     // shaderspriteuv = new
                                     // GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_sprite)),
                                     // Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_sprite)));
                                     shaders = new GLSLShader[7];
                                     shaders[0] = shaderbasic; // standard
                                     shaders[1] = shaderlighting; // not used?
                                     shaders[2] = shaderlightingbump; // ships
                                     shaders[3] = shaderbasicwcolor; // nebula
                                     shaders[4] = shaderlightingbumpwcolor; // planet
                                     shaders[5] = shadercorona; // corona
                                     shaders[6] = shaderbasiccoloronly; // atmosphere
                                     // shaders[7] = shaderspriteuv; // notused?

                                     // really bad Matt...how lazy is this!
                                     // shadersun1 =new
                                     // GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasicwcolor)),
                                     // Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasicwcolor)));
                                     // shadersun2 =new
                                     // GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasicwcolor)),
                                     // Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasicwcolor)));
                                     // shadersun3 =new
                                     // GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasicwcolor)),
                                     // Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasicwcolor)));
                                     // shadersun4 =new
                                     // GLSLShader(Loader.loadTextFile(res.openRawResource(R.raw.vertexshader_vbasicwcolor)),
                                     // Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader_vbasicwcolor)));

                                     Log.i(TAG, "TIME:(SHADERS READ):" + System.currentTimeMillis());
                     }

                     @Override
                     public void onSurfaceChanged(GL10 gl, int width, int height)
                     {
                                     if (fbuffer != null)
                                     {
                                                     fbuffer.dispose();
                                     }
                                     fbuffer = new FrameBuffer(width, height);

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

                     @Override
                     public void
onDrawFrame(GL10 gl)
                     {

                                     if (world != null && fbuffer != null && issetup == false)
                                     {
                                                     issetup = true;
                                                     createskybox();
                                                     skyboxcam = skyboxworld.getCamera();
                                                     skyboxcam.setFOV(1.5f); // play with this....

                                                     // load the relevant mesh.....

                                                     mymesh = new Mesh();
                                                     final String ext = ".jpg";
// modified 3-5-2015
                                                     final String ext2 = ".jpg";

                                                     String myext = ext;

                                                     if (meshid >= 0 && meshid < 20)
                                                     {
                                                                     if (meshid % 4 == 0)
                                                                     {
                                                                                     if (meshid == 8)
                                                                                     {
                                                                                                     myext = ext2;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     myext = ext;
                                                                                     }
                                                                                     mymesh.loadencryptedserializedobj(assets, "3dmodels/spaceships/cap" + (((meshid - (meshid % 4)) / 4) + 1) + ".dat", "3dmodels/spaceships/cap" + (((meshid - (meshid % 4)) / 4) + 1) + myext, 0, 111);

                                                                                     camdist = 300f;
                                                                                     if (meshid == 4)
                                                                                     {
                                                                                                     camdist = 200f;
                                                                                     }
                                                                     }
                                                                     if (meshid % 4 == 1)
                                                                     {
                                                                                     if (meshid == 9)
                                                                                     {
                                                                                                     myext = ext2;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     myext = ext;
                                                                                     }

                                                                                     mymesh.loadencryptedserializedobj(assets, "3dmodels/spaceships/crs" + (((meshid - (meshid % 4)) / 4) + 1) + ".dat", "3dmodels/spaceships/crs" + (((meshid - (meshid % 4)) / 4) + 1) + myext, 0, 111);
                                                                                     camdist = 200f;
                                                                                     if (meshid == 5)
                                                                                     {
                                                                                                     camdist = 150f;
                                                                                     }
                                                                     }
                                                                     if (meshid % 4 == 2)
                                                                     {
                                                                                     if (meshid == 18 || meshid == 14)
                                                                                     {
                                                                                                     myext = ext2;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     myext = ext;
                                                                                     }
                                                                                     mymesh.loadencryptedserializedobj(assets, "3dmodels/spaceships/bmbr" + (((meshid - (meshid % 4)) / 4) + 1) + ".dat", "3dmodels/spaceships/bmbr" + (((meshid - (meshid % 4)) / 4) + 1) + myext, 0, 111);
                                                                                     camdist = 40f;
                                                                     }
                                                                     if (meshid % 4 == 3)
                                                                     {
                                                                                     if (meshid == 15 || meshid == 19)
                                                                                     {
                                                                                                     myext = ext2;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     myext = ext;
                                                                                     }

                                                                                     mymesh.loadencryptedserializedobj(assets, "3dmodels/spaceships/fgtr" + (((meshid - (meshid % 4)) / 4) + 1) + ".dat", "3dmodels/spaceships/fgtr" + (((meshid - (meshid % 4)) / 4) + 1) + myext, 0, 111);
                                                                                     camdist = 35f;
                                                                     }
                                                     }
                                                     mymesh.obj.setRotationMatrix(Level3.ROTATE3DS.getRotationMatrix());
                                                     mymesh.obj.setVisibility(true);
                                                     mymesh.obj.setShader(shaders[2]);
                                                     world.addObject(mymesh.obj);

                                     }
                                     if (updatetime == 0)
                                     {
                                                     updatetime = System.currentTimeMillis();
                                     }
                                     if (camera == null && world != null)
                                     {
                                                     camera = world.getCamera();

                                     }

                                     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)
                                     {
                                                     updatetime = System.currentTimeMillis();
                                                     if (firstupdate)
                                                     {
                                                                     firstupdate = false;
                                                     }
                                                     if (camera != null)
                                                     {
                                                                     // move camera......
                                                                     if (campos == null)
                                                                     {
                                                                                     campos = new SimpleVector(0, 0.1f * camdist, camdist);
                                                                     }
                                                                     else
                                                                     {
                                                                                     cframe++;
                                                                                     campos.x = camdist * (float) Math.sin((float) cframe / 200.0);
                                                                                     campos.z = camdist * (float) Math.cos((float) cframe / 200.0);
                                                                                     campos.y = -30f + 0.1f * camdist + 0.25f * camdist * (float) Math.sin((float) (cframe) / 250.0);
                                                                     }
                                                                     camera.setPosition(campos);
                                                                     camera.lookAt(SimpleVector.ORIGIN);
                                                     }

                                                     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)
                                     {
                                                     fbuffer.clear();
                                                     if (skyboxworld != null)
                                                     {
                                                                     skyboxworld.renderScene(fbuffer);
                                                                     skyboxworld.draw(fbuffer);
                                                     }
                                                     fbuffer.clearZBufferOnly();
                                                     world.renderScene(fbuffer);
                                                     world.draw(fbuffer);
                                                     fbuffer.clearZBufferOnly();
                                                     if (uiworld != null)
                                                     {
                                                                     uiworld.renderScene(fbuffer);
                                                                     uiworld.draw(fbuffer);
                                                     }

                                                     fbuffer.display();
                                     }
                     }
     }

     private void initplanet()
     {
                     Texture ptex = null;
                     InputStream intex1 = null;
                     try
                     {
                                     int ppi = 1 + irand.nextInt(4);
                                     int ppj = (meshid - (meshid % 4)) / 4;
                                     if (ppj >= 0 && ppj < 5)
                                     {
                                                     ppi = ppj + 1;
                                                     if (ppj == 1)
                                                     {
                                                                     ppi = 3;
                                                     }
                                                     if (ppj == 2)
                                                     {
                                                                     ppi = 2;
                                                     }
                                     }
                                     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;
                                     }
                     }

                     try
                     {
                                     intex1 = assets.open("environment/box" + nc + "/skyboxbk.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 + "/skyboxdn.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 + "/skyboxfr.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 + "/skyboxlf.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 + "/skyboxrt.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 + "/skyboxup.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.....
                     try
                     {
                                     intex1 = assets.open("environment/box" + nc2 + "/skyboxbk.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 + "/skyboxdn.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 + "/skyboxfr.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 + "/skyboxlf.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 + "/skyboxrt.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 + "/skyboxup.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]);

                     // 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());
     }
}