// This code example is created for educational purpose
// by Thorsten Thormaehlen (contact: www.thormae.de).
// It is distributed without any warranty.

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.nio.FloatBuffer;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import javax.swing.JFrame;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.FPSAnimator;


class Renderer {

  private GLU glu = new GLU();

  public float t = 0.0f;
  public int mode = 0;

  private int[] bufID = new int[1];
  private int bufSize = 0;
  
  public void init(GLAutoDrawable d) {
    GL2 gl = d.getGL().getGL2(); // get the OpenGL graphics context
    gl.glEnable(GL2.GL_DEPTH_TEST);
    
    // generating VBO input data
    int ayimutSegs = 1000;
    int polarSegs = 1000;
    FloatBuffer dataIn = Buffers.newDirectFloatBuffer(ayimutSegs* polarSegs * 3);
    double ayimutStep = 2.0f * Math.PI / (double)ayimutSegs;
    double polarStep = Math.PI / (double)polarSegs;
    double r = 1.0f;
    bufSize = 0;
    for(int m=0; m < ayimutSegs; m++) {
      for(int n=0; n < polarSegs; n++) {
        double phi = ayimutStep*m;
        double theta = polarStep * n;

        // compute xyz from spherical coordinates
        float x = (float) (r * Math.sin(theta) * Math.cos(phi));
        float y = (float) (r * Math.sin(theta) * Math.sin(phi));
        float z = (float) (r * Math.cos(theta));

        dataIn.put(x);
        dataIn.put(y);
        dataIn.put(z);
        bufSize++;
      }
    }

    dataIn.flip();
    
    // generating VBO
    gl.glGenBuffers(1, bufID, 0);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufID[0]);
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, dataIn.capacity()*Buffers.SIZEOF_FLOAT, dataIn, GL2.GL_STATIC_DRAW);
  }


  
  public void resize(GLAutoDrawable d, int w, int h) {
    GL2 gl = d.getGL().getGL2(); // get the OpenGL 2 graphics context
    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective (30.0, (float)w/(float)h, 0.1, 50.0);
  }

  public void display(GLAutoDrawable d) {
    GL2 gl = d.getGL().getGL2();  // get the OpenGL 2 graphics context
    
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
    // set camera
    glu.gluLookAt(3.5, -1.0, 3.5, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
    // draw scene
    gl.glRotatef(t, 0.0f, 0.0f, 1.0f);

    // activating VBO
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufID[0]);
    int stride = 0;
    gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

    if(mode == 0) {
      gl.glColor3f(1.0f,1.0f,1.0f);
    }else{
      gl.glColorPointer(3, GL2.GL_FLOAT, stride, 0);
      gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
    }

    // render VBO
    gl.glDrawArrays(GL2.GL_POINTS, 0, bufSize);

    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
   
    gl.glFlush();
  }
}

class MyGui extends JFrame implements GLEventListener {

  private Renderer renderer;

  public void createGUI() {
    setTitle("Press 1 to use the VBO data for vertex colors as well");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GLProfile glp = GLProfile.getDefault();
    GLCapabilities caps = new GLCapabilities(glp);
    GLCanvas canvas = new GLCanvas(caps);
    setSize(320, 320);

    getContentPane().add(canvas);
    final FPSAnimator ani = new FPSAnimator(canvas, 60, true);
    canvas.addGLEventListener(this);
    setVisible(true);
    renderer = new Renderer();
    
    canvas.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent event) {
        boolean redraw = false;
        switch(event.getKeyCode()) {
          case '1':
            if(renderer.mode == 1) renderer.mode = 0;
            else renderer.mode = 1;
            redraw = true;
            break;
        }
        if(redraw) { // done automatically
        }
      }
    });
    
    
    ani.start();
  }

  @Override
  public void init(GLAutoDrawable d) {
    renderer.init(d);
  }

  @Override
  public void reshape(GLAutoDrawable d, int x, int y, int width, int height) {
    renderer.resize(d, width, height);
  }

  @Override
  public void display(GLAutoDrawable d) {
    float offset = 1.0f;
    renderer.t += offset;
    renderer.display(d);
  }

  @Override
  public void dispose(GLAutoDrawable d) {
  }
}

public class VboDrawArrays {
  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        MyGui myGUI = new MyGui();
        myGUI.createGUI();
      }
    });
  }
}
