illuminationtest.java

来自「基于java的3d开发库。对坐java3d的朋友有很大的帮助。」· Java 代码 · 共 552 行 · 第 1/2 页

JAVA
552
字号
//===========================================================================// Basic java classesimport java.io.File;import java.io.IOException;// AWT GUI java classesimport java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;// Swing GUI java classesimport javax.swing.JFrame;import javax.swing.JFileChooser;import javax.swing.JPanel;// JOGL classesimport javax.media.opengl.GL;import javax.media.opengl.GLAutoDrawable;import javax.media.opengl.GLCanvas;import javax.media.opengl.GLEventListener;// VSDK classesimport vsdk.toolkit.common.RendererConfiguration;import vsdk.toolkit.common.ColorRgb;import vsdk.toolkit.common.Vector3D;import vsdk.toolkit.common.Vertex;import vsdk.toolkit.common.Triangle;import vsdk.toolkit.environment.Camera;import vsdk.toolkit.environment.Light;import vsdk.toolkit.environment.geometry.TriangleMesh;import vsdk.toolkit.gui.CameraController;import vsdk.toolkit.gui.CameraControllerAquynza;import vsdk.toolkit.gui.RendererConfigurationController;import vsdk.toolkit.io.geometry.ReaderObj;import vsdk.toolkit.render.jogl.JoglCameraRenderer;import vsdk.toolkit.render.jogl.JoglLightRenderer;import vsdk.toolkit.render.jogl.JoglTriangleMeshRenderer;// Application classesimport util.filters.ObjectFilter;/** */public class IlluminationTest    extends JFrame implements GLEventListener, MouseListener,                   MouseMotionListener, MouseWheelListener, KeyListener {    private Camera camera;    private Light light1;    private Light light2;    private Light light3;    private CameraController cameraController;    private RendererConfiguration qualitySelection1;    private RendererConfiguration qualitySelection2;    private RendererConfigurationController qualityController;    private GLCanvas canvas;    private Vector3D lightPosition;    private boolean showVectors;    private Vector3D N;    private Vector3D H;    private double phongExp;    private TriangleMesh baseMesh;    private TriangleMesh surfaceMesh;    private int coord(int nx, int ny, int ix, int iy)    {        return ((nx+1)*iy) + ix;    }    private void updateH()    {        Vector3D L = new Vector3D(lightPosition);        L.normalize();        double mag = N.dotProduct(L) * 2;        H = (N.multiply(mag)).substract(L);    }    private TriangleMesh createFloorMesh()    {        //-----------------------------------------------------------------        // Parametric        double dx = 0.2;  // Size of each tile in x direction        double dy = 0.2;  // Size of each tile in y direction        int nx = 50;      // Number of tiles in the x direction        int ny = 50;      // Number of tiles in the y direction        double iniz = 0.1; // Distance of the floor down of the z=0 plane        // Temporary variable        double x;        double y;        int ix;        int iy;        int index;        //-----------------------------------------------------------------        Vertex v[] = new Vertex[(nx+1)*(ny+1)];        Vector3D k = new Vector3D(0, 0, 1);        index = 0;        for ( iy = 0, y = -((double)ny)/2*dy; iy <= ny; iy++, y += dy ) {            for ( ix = 0, x = -((double)nx)/2*dx; ix <= nx; ix++, x += dx ) {                v[index] = new Vertex(new Vector3D(x, y, 0), k);                index++;            }        }        //-----------------------------------------------------------------        Triangle t[] = new Triangle[nx*ny*2];        int a, b, c;        index = 0;        for ( iy = 0; iy < ny; iy++ ) {            for ( ix = 0; ix < nx; ix++ ) {                a = coord(nx, ny, ix, iy);                b = coord(nx, ny, ix+1, iy);                c = coord(nx, ny, ix+1, iy+1);                t[index] = new Triangle(a, b, c);                index++;                a = coord(nx, ny, ix, iy);                b = coord(nx, ny, ix+1, iy+1);                c = coord(nx, ny, ix, iy+1);                t[index] = new Triangle(a, b, c);                index++;            }        }        //-----------------------------------------------------------------        TriangleMesh m = new TriangleMesh();        m.setVertexes(v);        m.setTriangles(t);        m.calculateNormals();        return m;    }    private TriangleMesh createSurfaceMesh()    {        //-----------------------------------------------------------------        // Parametric        int nx = 30;      // Number of tiles in the x direction        int ny = 30;      // Number of tiles in the y direction        double iniz = 0.1; // Distance of the floor down of the z=0 plane        // Temporary variable        double x;        double y;        int ix;        int iy;        int index;        //- Prepare illumination model vectors ----------------------------        Vector3D L = new Vector3D(lightPosition);        L.normalize();        //-----------------------------------------------------------------        Vertex v[] = new Vertex[(nx+1)*(ny+1)];        double dtetha = 360.0 / ((double)ny);        double dphi = 90.0 / ((double)nx);        double tetha, phi, z;        double r, x1, y1, z1, d, s;        Vector3D E;        index = 0;        for ( iy = 0, tetha = 0; iy <= ny; iy++, tetha += dtetha ) {            for ( ix = 0, phi = 0; ix <= nx; ix++, phi += dphi ) {                // Constant (ambient)                //r = 1;                // Lambertian (diffuse) factor                d = 1.5*Math.max(0.0, N.dotProduct(L));                // Phong illumination model (specular) factor                x1 = Math.cos(Math.toRadians(tetha)) *                        Math.cos(Math.toRadians(phi));                y1 = Math.sin(Math.toRadians(tetha)) *                        Math.cos(Math.toRadians(phi));                z1 = Math.cos(Math.toRadians(90-phi));                E = new Vector3D(x1, y1, z1);                E.normalize();                s = Math.pow(H.dotProduct(E), phongExp);                r = d + s;                x = r * Math.cos(Math.toRadians(tetha)) *                        Math.cos(Math.toRadians(phi));                y = r * Math.sin(Math.toRadians(tetha)) *                        Math.cos(Math.toRadians(phi));                z = r * Math.cos(Math.toRadians(90-phi));                v[index] = new Vertex(new Vector3D(x, y, z));                index++;            }        }        //-----------------------------------------------------------------        Triangle t[] = new Triangle[nx*ny*2];        int a, b, c;        index = 0;        for ( iy = 0; iy < ny; iy++ ) {            for ( ix = 0; ix < nx; ix++ ) {                if ( iy < ny - 1 ) {                    a = coord(nx, ny, ix, iy);                    c = coord(nx, ny, ix+1, iy);                    b = coord(nx, ny, ix+1, iy+1);                    t[index] = new Triangle(a, b, c);                    index++;                    a = coord(nx, ny, ix, iy);                    c = coord(nx, ny, ix+1, iy+1);                    b = coord(nx, ny, ix, iy+1);                    t[index] = new Triangle(a, b, c);                    index++;                }                else {                    a = coord(nx, ny, ix, iy);                    c = coord(nx, ny, ix+1, iy);                    b = coord(nx, ny, ix+1, 0);                    t[index] = new Triangle(a, b, c);                    index++;                    a = coord(nx, ny, ix, iy);                    c = coord(nx, ny, ix+1, 0);                    b = coord(nx, ny, ix, 0);                    t[index] = new Triangle(a, b, c);                    index++;                }            }        }        //-----------------------------------------------------------------        TriangleMesh m = new TriangleMesh();        m.setVertexes(v);        m.setTriangles(t);        m.calculateNormals();        return m;    }    public IlluminationTest(String fileName) {        super("VITRAL mesh test - JOGL");        //-----------------------------------------------------------------        canvas = new GLCanvas();        canvas.addGLEventListener(this);        canvas.addMouseListener(this);        canvas.addMouseMotionListener(this);        canvas.addKeyListener(this);        this.add(canvas, BorderLayout.CENTER);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        camera = new Camera();        cameraController = new CameraControllerAquynza(camera);        qualitySelection1 = new RendererConfiguration();        qualitySelection2 = new RendererConfiguration();        qualityController = new RendererConfigurationController(qualitySelection1);        lightPosition = new Vector3D(0, 0, 3);        light1 = new Light(Light.POINT, lightPosition,                 new ColorRgb(1, 1, 1));        light1.setPosition(lightPosition);        showVectors = false;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?