/*
 * test.java
 *
 * Created on August 18, 2004, 12:58 PM
 */
import java.awt.*;
import java.awt.event.*;

import net.java.games.jogl.*;
import net.java.games.jogl.impl.*;

/**
 *
 * @author  user1
 */
public class DualHead {
    
    public static void main(String args[]) {
        DualHead dh = new DualHead();
    }
    
    /** Creates a new instance of test */
    public DualHead() {
        GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        for (int i = 0; i < gs.length; i++) {
            GraphicsConfiguration gc = gs[i].getDefaultConfiguration();
            System.err.println(gs[i]);
            Frame frame = new Frame(gc);
            frame.setSize(300 * (i + 1), 300);
            GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(
            new GLCapabilities(), null, null, gs[i]);
            canvas.addGLEventListener(new BasicRenderer(i));
            frame.add(canvas);
            final Animator animator = new Animator(canvas);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    animator.stop();
                    System.exit(0);
                }
            });
            frame.show();
            animator.start();
        }
    }
}
class BasicRenderer implements GLEventListener, MouseListener, MouseMotionListener {
    private float colorCycle = 0.0f;
    private int screen;
    
    public BasicRenderer(int screen) {
        this.screen = screen;
    }
    public void init(GLDrawable drawable) {
        GL gl = drawable.getGL();
        System.err.println("INIT GL IS: " + gl.getClass().getName());
    }
    
    public void reshape(GLDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        float h = (float)height / (float)width;
        
        gl.glMatrixMode(GL.GL_PROJECTION);
        
        System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
        System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
        System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
        System.err.println();
        System.err.println("glLoadTransposeMatrixfARB() supported: " +
        gl.isFunctionAvailable("glLoadTransposeMatrixfARB"));
        if (!gl.isFunctionAvailable("glLoadTransposeMatrixfARB")) {
            // --- not using extensions
            gl.glLoadIdentity();
        } else {
            // --- using extensions
            final float[] identityTranspose = new float[] {
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1
            };
            gl.glLoadTransposeMatrixfARB(identityTranspose);
        }
        gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -40.0f);
    }
    
    public void display(GLDrawable drawable) {
        GL gl = drawable.getGL();
        
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glBegin(gl.GL_TRIANGLES);
        
        if (screen == 0) {
            System.err.println("Screen 0");
            gl.glColor3f(1.0f, 0.0f, 1.0f);
        }
        else {
            gl.glColor3f(colorCycle, 0.0f, 0.0f);
            System.err.println("Screen 1");
        }
        gl.glVertex3f(-5.0f, -5.0f, 0.0f);
        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(5.0f, -5.0f, 0.0f);
        if (screen == 0) {
            gl.glColor3f(colorCycle, 0.0f, 1.0f);
        }
        else {
            gl.glColor3f(0.0f, 0.0f, 1.0f);
        }
        gl.glVertex3f(-5.0f, 5.0f, 0.0f);
        gl.glEnd();
        colorCycle += .05;
        if (colorCycle > 1.0f)
            colorCycle = 0.0f;
        
        try {
           Thread.sleep(100);
        }
        catch (InterruptedException e) {}
    }
    
    public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged){}
    // Methods required for the implementation of MouseListener
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseDragged(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
}