home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / ice / TextureTest.java < prev    next >
Encoding:
Java Source  |  1996-05-17  |  5.3 KB  |  196 lines

  1. // TextureTest.java
  2. //
  3. // Copyright (C) 1995 Dimension X, Inc.
  4. //   Chris Laurel 10-28-95
  5. //
  6. // This code may be redistributed freely.
  7.  
  8. import java.applet.Applet;
  9. import java.awt.*;
  10. import java.net.URL;
  11. import java.net.MalformedURLException;
  12. import ice.*;
  13.  
  14.  
  15. public class TextureTest extends Applet
  16. {
  17.    Graphics3D g3d;
  18.    Model cube;
  19.    float xangle, yangle;
  20.    int lastX, lastY;
  21.    Matrix4 t = new Matrix4();
  22.    Texture tex;
  23.  
  24.  
  25.    public void init()
  26.    {
  27.       cube = createBox(2, 2, 2);
  28.    }
  29.  
  30.    public void start()
  31.    {
  32.       createGraphicsContext();
  33.       if (tex == null) {
  34.      try {
  35.         Image texImage = getImage(new URL(getDocumentBase(), getParameter("image")));
  36.         tex = new Texture(g3d, texImage.getSource(), Texture.RGB);
  37.      } catch (MalformedURLException e) {
  38.         System.out.println("Bad URL");
  39.      }
  40.       }
  41.  
  42.       display();
  43.    }
  44.  
  45.    public synchronized void display()
  46.    {
  47.       // Clear the contents of the frame buffer
  48.       g3d.clear(Graphics3D.ColorBuffer);
  49.  
  50.       g3d.useTexture(tex);
  51.  
  52.       // Set up the model transformation
  53.       t.identity();
  54.       // rotate it
  55.       t.xrotate(xangle);
  56.       t.yrotate(yangle);
  57.       // translate the cube away from the camera along the z axis
  58.       t.translate(0, 0, -5);
  59.  
  60.       // render
  61.       g3d.concatModelMatrix(t);
  62.       g3d.renderSolid(cube);
  63.       g3d.popModelMatrix();
  64.  
  65.       repaint();
  66.    }
  67.  
  68.    
  69.    // Standard applet methods
  70.    public boolean mouseDown(Event e, int x, int y)
  71.    {
  72.       lastX = x;
  73.       lastY = y;
  74.       return true;
  75.    }
  76.  
  77.    public boolean mouseDrag(Event e, int x, int y)
  78.    {
  79.       yangle += (float) (x - lastX) / (float) size().width * (float) Math.PI;
  80.       xangle += (float) (y - lastY) / (float) size().height * (float) Math.PI;
  81.       lastX = x;
  82.       lastY = y;
  83.       display();
  84.       return true;
  85.    }
  86.  
  87.    public void paint(Graphics g)
  88.    {
  89.       update(g);
  90.    }
  91.  
  92.    public void update(Graphics g)
  93.    {
  94.       if (g3d != null)
  95.      g3d.paint(g, 0, 0);
  96.    }
  97.  
  98.  
  99.    private void createGraphicsContext()
  100.    {
  101.       // Create a new 3D graphics context . . . since we're just
  102.       //   going to be rendering a cube, we don't need a depth
  103.       //   buffer--all necessary hidden surface removal is handled
  104.       //   by backface culling.  Note that we need to have a
  105.       //   regular awt Graphics object in order to create a new
  106.       //   3D graphics context (this will likely change once
  107.       //   Iced Java is ported to the beta JDK.)
  108.       g3d = new Graphics3D(this,
  109.                Graphics3D.ColorBuffer,
  110.                size().width, size().height);
  111.  
  112.       // Set the clear color to be light gray (generally, the same
  113.       //   color as the main browser window)
  114.       g3d.setClearColor(Color.lightGray);
  115.  
  116.       g3d.disableLighting();
  117.       g3d.enableTexturing();
  118.       // g3d.setTextureMode(Graphics3D.TextureModeModulate);
  119.  
  120.       // Enable dithering, if necessary.  This will only have an effect
  121.       //   for a non-truecolor display.
  122.       g3d.enableDithering();
  123.    }
  124.  
  125.  
  126.    // Create a box model centered about the origin.
  127.    Model createBox(int width, int height, int depth)
  128.    {
  129.       try {
  130.      // Create a new, empty model with per vertex materials,
  131.      //   no normals (we're just using emissiveColor so we
  132.      //   don't need them for lighting), and no texture
  133.      //   coordinates;
  134.      Model m = new Model(Model.BindingPerVertex,
  135.                  Model.BindingNone,
  136.                  true);
  137.  
  138.      // Add the vertices
  139.      m.addVertex(-width / 2, -height / 2, depth / 2);
  140.      m.addVertex(-width / 2, height / 2, depth / 2);
  141.      m.addVertex(-width / 2, height / 2, -depth / 2);
  142.      m.addVertex(-width / 2, -height / 2, -depth / 2);
  143.      m.addVertex(width / 2, -height / 2, depth / 2);
  144.      m.addVertex(width / 2, height / 2, depth / 2);
  145.      m.addVertex(width / 2, height / 2, -depth / 2);
  146.      m.addVertex(width / 2, -height / 2, -depth / 2);
  147.  
  148.      // Add the materials
  149.      Material mat = new Material(Color.white, Color.black,
  150.                      Color.black, Color.black,
  151.                      1, 1);
  152.  
  153.      mat.emissive = Color.blue;    m.addMaterial(mat);
  154.      mat.emissive = Color.red;     m.addMaterial(mat);
  155.      mat.emissive = Color.green;   m.addMaterial(mat);
  156.      mat.emissive = Color.white;   m.addMaterial(mat);
  157.      mat.emissive = Color.cyan;    m.addMaterial(mat);
  158.      mat.emissive = Color.yellow;  m.addMaterial(mat);
  159.      mat.emissive = Color.magenta; m.addMaterial(mat);
  160.      mat.emissive = Color.gray;    m.addMaterial(mat);
  161.  
  162.      // Add the texture coordinates
  163.      m.addTextureCoord(0, 0);
  164.      m.addTextureCoord(1, 0);
  165.      m.addTextureCoord(1, 1);
  166.      m.addTextureCoord(0, 1);
  167.  
  168.      // Add the faces.  vi is an array which will contain the
  169.      //   vertex indices for each face.  Since each vertex is
  170.      //   mapped to one material, we can use the same array
  171.      //   for the material indices.
  172.      int vi[] = new int[4];
  173.      int ti[] = new int[4];
  174.      ti[0] = 0; ti[1] = 1; ti[2] = 2; ti[3] = 3;
  175.      vi[0] = 0; vi[1] = 1; vi[2] = 2; vi[3] = 3;
  176.      m.polygon(4, vi, vi, null, ti); // left (-X)
  177.      vi[0] = 7; vi[1] = 6; vi[2] = 5; vi[3] = 4;
  178.      m.polygon(4, vi, vi, null, ti); // right (+X)
  179.      vi[0] = 4; vi[1] = 5; vi[2] = 1; vi[3] = 0;
  180.      m.polygon(4, vi, vi, null, ti); // front (+Z)
  181.      vi[0] = 3; vi[1] = 2; vi[2] = 6; vi[3] = 7;
  182.      m.polygon(4, vi, vi, null, ti); // back (-Z)
  183.      vi[0] = 1; vi[1] = 5; vi[2] = 6; vi[3] = 2;
  184.      m.polygon(4, vi, vi, null, ti); // top (+Y)
  185.      vi[0] = 3; vi[1] = 7; vi[2] = 4; vi[3] = 0;
  186.      m.polygon(4, vi, vi, null, ti); // bottom (-Y)
  187.  
  188.      return m;
  189.       } catch (InvalidModelException e) {
  190.      return null;
  191.       }
  192.  
  193.    }
  194.  
  195. }
  196.