/*
  Copyright (c) 2004, Bradley 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 java.awt.*;
import java.awt.image.*;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * An applet that draws a textured square to the screen
 */
public class MultitexturingApplet extends JApplet implements GLEventListener {
  public void init() {
    GLCapabilities glc = new GLCapabilities();

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

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

    gl.glColor3f(1.0f1.0f1.0f);
    // send down the geometry
    gl.glBegin(GL.GL_TRIANGLE_STRIP);

    gl.glMultiTexCoord2f(GL.GL_TEXTURE0_ARB, 0.0f1.0f);
    gl.glMultiTexCoord2f(GL.GL_TEXTURE1_ARB, 0.0f1.0f);
    gl.glVertex3f(-0.5f, -0.5f0.0f);

    gl.glMultiTexCoord2f(GL.GL_TEXTURE0_ARB, 1.0f1.0f);
    gl.glMultiTexCoord2f(GL.GL_TEXTURE1_ARB, 1.0f1.0f);
    gl.glVertex3f(0.5f, -0.5f0.0f);

    gl.glMultiTexCoord2f(GL.GL_TEXTURE0_ARB, 0.0f0.0f);
    gl.glMultiTexCoord2f(GL.GL_TEXTURE1_ARB, 0.0f0.0f);
    gl.glVertex3f(-0.5f0.5f0.0f);

    gl.glMultiTexCoord2f(GL.GL_TEXTURE0_ARB, 1.0f0.0f);
    gl.glMultiTexCoord2f(GL.GL_TEXTURE1_ARB, 1.0f0.0f);
    gl.glVertex3f(0.5f0.5f0.0f);

    gl.glEnd();

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

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

    // create the checkerboard image
    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);

    // generate the texture name
    int[] texNames = new int[2];
    gl.glGenTextures(2, texNames);

    gl.glActiveTextureARB(GL.GL_TEXTURE0_ARB);
    loadTexture(gl, texNames[0]"KFM.jpg");

    gl.glActiveTextureARB(GL.GL_TEXTURE1_ARB);
    loadTexture(gl, texNames[1]"bg.jpg");
  }

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

  public void loadTexture(GL gl, int texName, String fileName) {
    try {
      URL url = new URL(getCodeBase(), fileName);
      Image img = getImage(url);

      PixelGrabber pg = new PixelGrabber(img, 00256256true);
      boolean status = pg.grabPixels();
      if (status == false) {
        System.err.println(
          "Error, unable to grab pixels from:" + fileName);
      }
      gl.glBindTexture(GL.GL_TEXTURE_2D, texName);

      // set some texture parameters
      gl.glTexParameteri(
        GL.GL_TEXTURE_2D,
        GL.GL_TEXTURE_WRAP_S,
        GL.GL_REPEAT);
      gl.glTexParameteri(
        GL.GL_TEXTURE_2D,
        GL.GL_TEXTURE_WRAP_T,
        GL.GL_REPEAT);
      gl.glTexParameteri(
        GL.GL_TEXTURE_2D,
        GL.GL_TEXTURE_MAG_FILTER,
        GL.GL_LINEAR);
      gl.glTexParameteri(
        GL.GL_TEXTURE_2D,
        GL.GL_TEXTURE_MIN_FILTER,
        GL.GL_LINEAR);

      Object obj = pg.getPixels();

      int[] inRaster = (int[]) pg.getPixels();
      byte[] outRaster = new byte[inRaster.length * 4];
      for (int i = 0; i < inRaster.length; ++i) {
        int pixel = inRaster[i];
        int alpha = (pixel >> 240xff;
        int red = (pixel >> 160xff;
        int green = (pixel >> 80xff;
        int blue = (pixel0xff;
        outRaster[i * 0(bytered;
        outRaster[i * 1(bytegreen;
        outRaster[i * 2(byteblue;
        outRaster[i * 3(bytealpha;
      }

      // load the image into the graphics card
      gl.glTexImage2D(
        GL.GL_TEXTURE_2D,
        0,
        GL.GL_RGBA,
        256,
        256,
        0,
        GL.GL_RGBA,
        GL.GL_UNSIGNED_BYTE,
        outRaster);

      gl.glEnable(GL.GL_TEXTURE_2D);
    catch (InterruptedException ie) {
      ie.printStackTrace();
    catch (MalformedURLException murle) {
      murle.printStackTrace();
    }

  }
}