
// External imports
import java.awt.*;
import java.awt.event.*;


import com.jogamp.opengl.util.*;

import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import javax.swing.JFrame;

/**
 * Example application that demonstrates how to put together a single-threaded
 * rendering system.
 */
public class JOGL_Lines extends JFrame
    implements GLEventListener
{
    private GLAutoDrawable canvas;
    private double theta = 0;
    private double s = 0;
    private double c = 0;

    public JOGL_Lines()
    {
        super("Basic JOGL Demo");

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        setLayout(new BorderLayout());

        setSize(400, 400);
        setLocation(40, 40);

        // Need to set visible first before starting the rendering thread due
        // to a bug in JOGL. See JOGL Issue #54 for more information on this.
        // http://jogl.dev.java.net
        setVisible(true);

        setupJOGL();

        
    }

    //---------------------------------------------------------------
    // Methods defined by GLEventListener
    //---------------------------------------------------------------

    /**
     * Called by the drawable immediately after the OpenGL context is
     * initialized; the GLContext has already been made current when
     * this method is called.
     *
     * @param drawable The display context to render to
     */
    public void init(GLAutoDrawable drawable)
    {

    }

    /**
     * Called by the drawable when the surface resizes itself. Used to
     * reset the viewport dimensions.
     *
     * @param drawable The display context to render to
     */
    public void reshape(GLAutoDrawable drawable,
                        int x,
                        int y,
                        int width,
                        int height)
    {
    }

    /**
     * Called by the drawable when the display mode or the display device
     * associated with the GLDrawable has changed
     */
    public void displayChanged(GLAutoDrawable drawable,
                               boolean modeChanged,
                               boolean deviceChanged)
    {
    }

    /**
     * Called by the drawable to perform rendering by the client.
     *
     * @param drawable The display context to render to
     */
    public void display(GLAutoDrawable drawable)
    {
        update();
        render(drawable);            
    }

    private void update() {
        theta += 0.01;
        s = Math.sin(theta);
        c = Math.cos(theta);
    }

	public void dispose(GLAutoDrawable drawable) {
	}

    private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);      

        gl.glShadeModel(GL2.GL_FLAT);
        gl.glColor3f(1, 0, 0);

        gl.glBegin(GL.GL_TRIANGLES);
        gl.glVertex2d(-c, -c);
        gl.glVertex2d(0, c);
        gl.glVertex2d(s, -s);
        gl.glEnd();
        
        gl.glColor3f(1, 1, 1);

        gl.glBegin(GL.GL_LINES);
        gl.glVertex2d(-c, -c);
        gl.glVertex2d(0, c); 
        gl.glVertex2d(0, c);
        gl.glVertex2d(s, -s);
        gl.glVertex2d(s, -s);
        gl.glVertex2d(-c, -c);
        gl.glEnd();
    }    
    
    
    //---------------------------------------------------------------
    // Local methods
    //---------------------------------------------------------------

    /**
     * Create the basics of the JOGL screen details.
     */
    private void setupJOGL()
    {
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        caps.setDoubleBuffered(true);
        caps.setHardwareAccelerated(true);
        
        // Toggle this flag to true to see bug.
        boolean enableSuperSampling = true;        
        if (enableSuperSampling)
        {
            caps.setNumSamples(8);
            caps.setSampleBuffers(true);
        }

        boolean useSwingViewer = this instanceof JFrame; // needed because we have a swing JFrame
        canvas = useSwingViewer ?  new GLJPanel(caps) : new GLCanvas(caps);        
        canvas.addGLEventListener(this);
        canvas.setAutoSwapBufferMode(false);

        this.getContentPane().add((Component) canvas, BorderLayout.CENTER);

        FPSAnimator animator = new FPSAnimator(canvas, 60);
        animator.add(canvas);
        animator.start();
    }

    public static void main(String[] args)
    {
        JOGL_Lines demo = new JOGL_Lines();
        demo.setVisible(true);
    }
}
