/*
  Copyright (c) 2004, Bradley L. Jones
  All rights reserved.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  * Neither the name of the nondot.org nor the names of its contributors may 
    be used to endorse or promote products derived from this software without
    specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
*/
package org.nondot.applets;

import javax.swing.JApplet;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;

import java.awt.*;

/**
 * An applet that does simple lighting
 */
public class SimpleLightingApplet extends JApplet implements GLEventListener {
  GLUT glut = new GLUT();
  float[] matAmbient = .75f.3f0.0f1.0f };
  float[] matDiffuse = .75f.3f0.0f1.0f };
  float[] matSpecular = 1.0f1.0f1.0f1.0f };
  ;
  float[] matShininess = 25.0f };
  float[] lightAmbient = .15f.15f.15f1.0f };
  float[] lightPosition = 4.0f4.0f5.0f1.0f };

  public void init() { 
    GLCapabilities glc = new GLCapabilities();
    glc.setDoubleBuffered(false);

    GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(glc);

    getContentPane().add(canvas, BorderLayout.CENTER);
    canvas.addGLEventListener(this);
  }

  public void display(GLDrawable gld) {
    // the drawing code
    GL gl = gld.getGL();
    GLU glu = gld.getGLU();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    // setup the camera
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    glu.gluLookAt(0.00.02.00.00.00.00.01.00.0);

    // set the light position
    gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition);

    // send down the geometry
    // this has normals at each vertex
    glut.glutSolidTorus(gl, .51.03020);

    gl.glFlush();
  }

  public void displayChanged(GLDrawable gld, boolean arg1, boolean arg2) {
  }

  public void init(GLDrawable gld) {
    // make sure we don't do anything wrong with OpenGL
    gld.setGL(new DebugGL(gld.getGL()));
    
    GL gl = gld.getGL();
    // set the clear color, this only needs to be done once at the beginning
    // of the program after the GL context has been created
    gl.glClearColor(0.0f0.0f0.75f1.0f);

    // enable the depth buffer
    gl.glEnable(GL.GL_DEPTH_TEST);

    // set up smooth shading
    gl.glShadeModel(GL.GL_SMOOTH);

    // setup lighting
    gl.glEnable(GL.GL_LIGHTING);
    gl.glEnable(GL.GL_LIGHT0);

    gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, matAmbient);
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, matDiffuse);
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, matSpecular);
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, matShininess);

    gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient);
  }

  public void reshape(GLDrawable gld, int x, int y, int w, int h) {
    // setup the projection matrix
    GL gl = gld.getGL();
    GLU glu = gld.getGLU();
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(90.0(doublew / h, .1100);

    gl.glMatrixMode(GL.GL_MODELVIEW);
  }
}