/*
 * Copyright (C) 2007 askywhale
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License (version 2)
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 */

package com.askywhale.rings;

import java.awt.Graphics;
import java.awt.Image;
import java.io.*;

import javax.swing.JFrame;

import net.sf.j3d.threeD.io.*;
import net.sf.j3d.threeD.raytracer.camera.*;
import net.sf.j3d.threeD.raytracer.engine.*;
import net.sf.j3d.util.graphics.GraphicsConverter;
import net.sf.j3d.util.graphics.RGB;
import net.sf.j3d.util.Vector;
import net.sf.j3d.threeD.raytracer.lighting.*;
import net.sf.j3d.threeD.raytracer.primitives.Plane;


public class Test2IsoSurf {
  /*
    private static void test(){
        TestSurface s = new TestSurface(new Vector(0,0,0),1);
        s.setRotationCoefficients(Math.PI,0,0);
        Ray r = new Ray(new Vector(0,0,2),new Vector(0,0,-1));
        System.out.println("..."+s.intersectAt(r));        
    }
    */
    public static void main(String[] args) {

        int width = 300;
        int height = 200;
        int ssWidth = 2;
        int ssHeight = 2;
        System.out.println("Starting 2 ...");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) { }        
        Surface surfaces[] = {               
                new Plane(Plane.XY,new RGB(.4,.8,.2)),
                new SimpleSymetricIsoSurface(new Vector(-4,0,1),1,
                		new RGB(1,1,1),SimpleSymetricIsoSurface.CROSS),
                new SimpleSymetricIsoSurface(new Vector(0,0,1),1,
                   		new RGB(1,1,1),SimpleSymetricIsoSurface.DBL_CONE),
                new SimpleSymetricIsoSurface(new Vector(4,0,1),1,
                   		new RGB(1,1,1),SimpleSymetricIsoSurface.STAR_6),                   		
                new SimpleSymetricIsoSurface(new Vector(-4,0,4),1,
                		new RGB(1,1,1),SimpleSymetricIsoSurface.OCTAEDRON),
                new SimpleSymetricIsoSurface(new Vector(0,0,4),1,
                   		new RGB(1,1,1),SimpleSymetricIsoSurface.GOO_SPHERE),
                new SimpleSymetricIsoSurface(new Vector(4,0,4),1,
                   		new RGB(1,1,1),SimpleSymetricIsoSurface.MOEBIUS),                   		
                };
        
        Light lights[] = {new AmbientLight(.5), 
                new DirectionalAmbientLight( //soleil
                        .7,
                        new RGB(1,1,.5),
                        new Vector(3,1,-1)),
                new DirectionalAmbientLight( //ciel
                        .1,
                        new RGB(.9,.9,1),
                        new Vector(0,0,-1)),                        
                new DirectionalAmbientLight( //reflexion verte
                        .1,
                        new RGB(.4,.8,.2),
                        new Vector(0,0,1))
                        };
        PinholeCamera camera = new PinholeCamera();
        camera.setFocalLength(.2);
        camera.setLocation(new Vector(0,-6,1.25));
        camera.setViewDirection(camera.getLocation().
                add(new Vector(0,0,-2)).multiply(-1));
        camera.setUpDirection(new Vector(0,0,1));
        Scene scene = new Scene(camera, lights, surfaces);
        
        RGB image[][] = RayTracingEngine.render(scene, width, height, ssWidth, ssHeight, null);
        try {
            FileEncoder.encodeImageFile(image, 
                    new File("test\\test2.jpeg"), 
                    FileEncoder.JPEGEncoding);
            Image im = GraphicsConverter.convertToAWTImage(image);
            new JFrame(){
                Image im;           
                public JFrame init(Image im){ 
                    this.im = im; setVisible(true);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    pack(); return this;                    
                }
                public void paint(Graphics gr) {                 
                    gr.drawImage(im,0,0,null);
                }
            }.init(im).setSize(width,height);
        } catch (Exception e) { e.printStackTrace(); }
        System.out.println("done !");
    }
}
