home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / DirectX / d3drmcastle / CastleCanvas.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  9.5 KB  |  415 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.*;
  5. import java.net.URL;
  6. import java.util.Vector;
  7. import com.ms.com.*;
  8. import com.ms.directX.*;
  9.  
  10. public class CastleCanvas extends Canvas implements DirectXConstants, ILoadTextureCallback
  11. {
  12.     //////////////////////////////////////////////////////////////////////////
  13.     public URL                        url;
  14.     public DirectDraw                dd;                    
  15.     public DirectDrawClipper        ddclipper;
  16.     public DirectDrawSurface        ddsprimary;
  17.     public DirectDrawSurface        ddsoffscreen;    
  18.     public DirectDrawSurface        ddszbuffer;
  19.     public DirectDrawPalette        ddpal;
  20.     public Direct3d                    d3d;
  21.     public Direct3dDevice            d3ddevice;
  22.     public Direct3dRM                d3drm;
  23.     public Direct3dRMDevice            d3drmdevice;
  24.     public Direct3dRMViewport        d3drmviewport;
  25.        public Direct3dRMFrame            mainframe;
  26.     public Direct3dRMMeshBuilder    builder;
  27.     public Direct3dRMFrame            scene;
  28.     public Direct3dRMFrame            camera;
  29.  
  30.     public float rx,ry,rz,rtheta;
  31.     public float px,py,pz;
  32.     public int keydown;
  33.  
  34.     private boolean running = true;
  35.  
  36.     public DirectInput    joy;
  37.     public JoyInfo        joypos;
  38.     public JoyCaps        joycaps;
  39.     public int            joyxrange;
  40.     public int            joyyrange;
  41.  
  42.     D3dVector            pos;
  43.     D3dVector            dir;
  44.     D3dVector            keydir;
  45.     D3dVector            up;
  46.     D3dVector            right;
  47.     D3dVector            zaxis;
  48.  
  49.     Rect                src;
  50.     
  51.     String                path;
  52.     String                xfile = "castle.x";
  53.  
  54.     //////////////////////////////////////////////////////////////////////////
  55.     public Dimension preferredSize() 
  56.     {
  57.         return new Dimension(518,384);
  58.     }
  59.  
  60.     //////////////////////////////////////////////////////////////////////////
  61.     public void init(URL url)
  62.     {
  63.         this.url = url;
  64.  
  65.         src = new Rect();
  66.         Dimension s = size();
  67.         src.top        = 0;
  68.         src.left    = 0;
  69.         src.right   = s.width;
  70.         src.bottom  = s.height;
  71.  
  72.         createDevice();
  73.  
  74.         pos   = new D3dVector();
  75.         dir   = new D3dVector();
  76.         keydir= new D3dVector();
  77.         up    = new D3dVector();
  78.          right = new D3dVector();
  79.  
  80.         joy      = new DirectInput();
  81.         joypos   = new JoyInfo();
  82.         joycaps  = new JoyCaps();
  83.  
  84.         //
  85.         // Get the Capabilities of the joy stick
  86.         //
  87.         try 
  88.         {
  89.             joy.getDevCaps(JOYSTICKID1,joycaps);
  90.  
  91.             //
  92.             // Set the axis's I'm interested in
  93.             //
  94.             joypos.flags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNBUTTONS;
  95.  
  96.             //
  97.             // determine the range of useful values 
  98.             //
  99.             joyxrange = (joycaps.xMax - joycaps.xMin)/2;
  100.             joyyrange = (joycaps.yMax - joycaps.yMin)/2;
  101.  
  102.         }
  103.         catch( Exception e )
  104.         {
  105.             joypos.flags = 0;
  106.             joyxrange = (joycaps.xMax - joycaps.xMin)/2;
  107.             joyyrange = (joycaps.yMax - joycaps.yMin)/2;
  108.         }
  109.     }
  110.  
  111.     //////////////////////////////////////////////////////////////////////////
  112.     private void createDevice()
  113.     {
  114.         Dimension s = size();
  115.  
  116.         dd    = new DirectDraw();
  117.         d3drm = new Direct3dRM();
  118.  
  119.         dd.setCooperativeLevel(this, DDSCL_NORMAL);
  120.  
  121.         ddclipper = dd.createClipper(0);
  122.         ddclipper.setComponent(this);
  123.  
  124.         //
  125.         // Create the primary surface
  126.         //
  127.         DDSurfaceDesc ddsd = new DDSurfaceDesc();
  128.         ddsd.flags   = DDSD_CAPS;
  129.         ddsd.ddsCaps = DDSCAPS_PRIMARYSURFACE;
  130.         ddsprimary     = dd.createSurface(ddsd);
  131.  
  132.         //
  133.         // Create and Set the clipper for the primary surface
  134.         //
  135.         ddsprimary.setClipper( ddclipper );
  136.  
  137.         //
  138.         // Create an offscreen rendering surface
  139.         //
  140.         ddsd.flags   = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  141.         ddsd.ddsCaps = DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
  142.         ddsd.width   = s.width;
  143.         ddsd.height  = s.height;
  144.         ddsoffscreen = dd.createSurface(ddsd);
  145.  
  146.         ddsd.flags   = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_ZBUFFERBITDEPTH;
  147.         ddsd.ddsCaps = DDSCAPS_ZBUFFER |  DDSCAPS_SYSTEMMEMORY;
  148.         ddsd.width   = s.width;
  149.         ddsd.height  = s.height;
  150.         ddsd.zbufferBitDepth = 16;
  151.         ddszbuffer     = dd.createSurface(ddsd);
  152.  
  153.         ddsoffscreen.addAttachedSurface(ddszbuffer);
  154.  
  155.         //
  156.         // This will attach the system palette to the offscreen surface
  157.         //
  158.         try
  159.         {
  160.             ddpal = dd.createPalette(DDPCAPS_8BIT | DDPCAPS_INITIALIZE, (byte[])null);
  161.             if( ddpal != null)
  162.             {
  163.                 ddsprimary.setPalette(ddpal);
  164.                 ddsoffscreen.setPalette(ddpal);
  165.             }
  166.            }
  167.            catch(Exception e)
  168.            {
  169.             // Failed palette probably not in 8-bit mode
  170.            }
  171.  
  172.  
  173.         d3d = dd.createD3D();
  174.  
  175.         _Guid g;
  176.         try
  177.         {
  178.             g = d3d.findDeviceForColorModel(D3DCOLOR_MONO, 0);
  179.         }
  180.            catch(Exception e)
  181.            {
  182.             // Try again just in case we are running on Minidrivers that 
  183.             // don't support MOMO modes 
  184.             g = d3d.findDeviceForColorModel(D3DCOLOR_RGB, 0);
  185.            }
  186.  
  187.         // 
  188.         // Create the D3D Device object associated 
  189.         // with the offscreen surface
  190.         //
  191.         d3ddevice = ddsoffscreen.create3DDevice(g);
  192.  
  193.         d3drmdevice = d3drm.createDeviceFromD3D(d3d,d3ddevice);
  194.  
  195.         switch (d3d.systemBpp())
  196.         {
  197.             case 1:
  198.                 d3drmdevice.setShades(4);
  199.                 d3drm.setDefaultTextureShades(4);
  200.                 break;
  201.                 
  202.             case 16:
  203.                 d3drmdevice.setShades(32);
  204.                 d3drm.setDefaultTextureColors(64);
  205.                 d3drm.setDefaultTextureShades(32);
  206.                 d3drmdevice.setDither(0);
  207.                 break;
  208.                 
  209.             case 24:
  210.             case 32:
  211.                 d3drmdevice.setShades(256);
  212.                 d3drm.setDefaultTextureColors(64);
  213.                 d3drm.setDefaultTextureShades(256);
  214.                 d3drmdevice.setDither(0);
  215.                 break;
  216.                 
  217.             default:
  218.                 d3drmdevice.setShades(1);
  219.                 d3drmdevice.setDither(0);
  220.                 d3drm.setDefaultTextureShades(1);
  221.                 break;
  222.         }
  223.  
  224.         d3drmdevice.setQuality(D3DRMRENDER_UNLITFLAT);
  225.         d3drmdevice.setTextureQuality( D3DRMTEXTURE_NEAREST );
  226.         
  227.         createScene();
  228.         d3drmviewport = d3drm.createViewport(d3drmdevice, camera, 0, 0, d3drmdevice.getWidth(), d3drmdevice.getHeight());
  229.         d3drmviewport.setBack(5000.0F);
  230.     }
  231.  
  232.     //////////////////////////////////////////////////////////////////////////
  233.     public Direct3dRMTexture callbackLoadTexture(String name, IUnknown args)
  234.     {
  235.         int ext = name.lastIndexOf(".");
  236.  
  237.         if (ext != -1) 
  238.         {
  239.             String fileExtension = name.substring(ext);
  240.             fileExtension.toLowerCase();
  241.             if( fileExtension.equals(".ppm") == true || fileExtension.equals(".bmp") == true)
  242.             {
  243.                 if ( path != null )
  244.                     name = path.concat(name);
  245.  
  246.                 return d3drm.loadTexture(name);
  247.             }
  248.         }
  249.         
  250.         return null;
  251.     }
  252.     
  253.     //////////////////////////////////////////////////////////////////////////
  254.     private void createScene()
  255.     {
  256.         String file;
  257.  
  258.         if ( url != null )    
  259.         {
  260.  
  261.             path  = url.toString();
  262.             path  = path.substring(6);
  263.             path  = path.replace('/','\\');
  264.  
  265.             // This is a check for when running under appeletviewer 
  266.             // it puts a '.' at the end of path 
  267.             if ( path.endsWith("\\.") )
  268.             {
  269.                 int index = path.lastIndexOf('.');
  270.                 path = path.substring(0,index);
  271.             }
  272.         
  273.             file = path.concat(xfile);
  274.         }
  275.         else
  276.         {
  277.             file = xfile.toString();
  278.         }
  279.  
  280.         scene = d3drm.createFrame(null);
  281.         scene.setSceneBackgroundRGB(1.0f,1.0f,1.0f);
  282.  
  283.         mainframe = d3drm.createFrame(null);
  284.         mainframe.setRotation(scene, 0.0F, 1.0F, 0.0F, 0.0F);
  285.         mainframe.setPosition(scene, 0.0F, 0.0F, -10.0F);
  286.         mainframe.setSortMode(D3DRMSORT_BACKTOFRONT); 
  287.         mainframe.loadFromFileByPos(file, 0, 0, (ILoadTextureCallback)this, null);
  288.  
  289.         builder = d3drm.createMeshBuilder();
  290.         builder.setPerspective(1);
  291.         builder.setQuality(D3DRMRENDER_UNLITFLAT);
  292.         builder.setColorSource(D3DRMCOLOR_FROMFACE);
  293.         builder.addFrame(mainframe);
  294.         builder.setColor(-1);
  295.  
  296.         scene.addVisualMeshBuilder(builder);
  297.  
  298.         camera = d3drm.createFrame(scene);
  299.         camera.setPosition(scene, 0.0F, 0.0F, 0.0F);
  300.     }
  301.  
  302.     //////////////////////////////////////////////////////////////////////////
  303.     public void renderScene() 
  304.     {
  305.         scene.move(1.0F);
  306.         d3drmviewport.clear();
  307.         d3drmviewport.render(scene);
  308.  
  309.         //
  310.         // Blit the Offscreen surface 
  311.         // 
  312.            ddsprimary.blt(src, ddsoffscreen, src, DDBLT_WAIT);
  313.  
  314.  
  315.         if ( joypos.flags != 0 )    // only check joystick if it's connected 
  316.         {
  317.             try
  318.             {
  319.                 joy.getPos(0,joypos);
  320.             }
  321.             catch( Exception e )
  322.             {
  323.                 // ... just in case joystick is not working continue to run 
  324.                 // keep app alive even if joystick is eith missing or offline somehow.
  325.                 joypos.xPos = 0;
  326.                 joypos.yPos = 0;
  327.             }
  328.         }
  329.  
  330.         int xpos = (joypos.xPos - joyxrange)/10;
  331.         int ypos = (joypos.yPos - joyyrange)/10;
  332.  
  333.         camera.getOrientation(scene, dir, up);
  334.         camera.getPosition(scene, pos);
  335.         d3drm.vectorCrossProduct(right, up, dir);
  336.  
  337.         //
  338.         // Handle rotation 
  339.         //
  340.         if ( xpos < -150 )
  341.         {
  342.             rtheta = 0.2F;
  343.             ry     = -up.y;
  344.         }
  345.         else if ( xpos > 150 )
  346.         {
  347.             rtheta = 0.2F;
  348.             ry     = up.y;
  349.         }
  350.         else if ( (keydown & 0x33) == 0 )
  351.         {
  352.             rtheta = 0.0F;
  353.             ry     = 0.0F;
  354.         }
  355.  
  356.         if ( ypos < -150 )
  357.         {
  358.             rtheta = 0.1F;
  359.             rx     = right.x;
  360.             rz     = right.z;
  361.         }
  362.         else if ( ypos > 150 )
  363.         {
  364.             rtheta = 0.1F;
  365.             rx     = -right.x;
  366.             rz     = -right.z;
  367.         }
  368.         else if ( (keydown & 0x33) == 0 )
  369.         {
  370.             rx     = 0.0F;
  371.             rz     = 0.0F;
  372.         }
  373.  
  374.         camera.setRotation(scene, rx, ry, rz, rtheta);
  375.  
  376.         // 
  377.         // level off joystick 
  378.         //
  379.         if ( rx == 0.0 && ry == 0.0 )
  380.             camera.setOrientation(scene, dir.x, dir.y, dir.z, 0,1,0 );
  381.  
  382.         if ( joypos.buttons > 0 )
  383.         {
  384.             if ( (joypos.buttons & JOY_BUTTON1) > 0 )
  385.             {
  386.                 pos.x += (dir.x * 1.3F);
  387.                 pos.z += (dir.z * 1.3F);
  388.             }
  389.             else
  390.             {
  391.                 pos.x += (dir.x * -1.3F);
  392.                 pos.z += (dir.z * -1.3F);
  393.             }
  394.         }
  395.         else if ( (keydown  & 0xc) > 0)
  396.         {
  397.             pos.x += keydir.x;
  398.             pos.z += keydir.z;
  399.         }
  400.  
  401.         if (pos.x < -28)
  402.             pos.x = -28;
  403.         else if ( pos.x > 21 )
  404.             pos.x = 21;
  405.  
  406.         if (pos.z < -17)
  407.             pos.z = -17;
  408.         else if ( pos.z > 23 )
  409.             pos.z = 23;
  410.         
  411.         camera.setPosition(scene, pos.x,pos.y,pos.z);
  412.     }
  413. }
  414.  
  415.