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

  1. // 
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.*;
  5. import com.ms.com.*;
  6. import com.ms.directX.*;
  7.  
  8. public class FlipCubeCanvas extends Canvas implements DirectXConstants
  9. {
  10.     // Private members 
  11.     Rect src = new Rect();            
  12.     Rect dst = new Rect();
  13.  
  14.     // Direct Draw Objects
  15.     DirectDraw                DD;                    // DirectDraw object
  16.     DirectDrawSurface        DDSPrimary;            // DirectDraw primary surface
  17.     DirectDrawClipper        DDClipper;            // Clipper for primary surface
  18.     DirectDrawSurface        DDSOffscreen;        // Offscreen rendering surface 
  19.     DirectDrawPalette        DDPal;                // DirectDraw palette
  20.  
  21.  
  22.     // Direct 3D Objects
  23.     Direct3d                D3D;
  24.     Direct3dDevice            D3DDevice;
  25.     Direct3dExecuteBuffer    D3DExBuf;
  26.     Direct3dViewport        D3DView;
  27.     Direct3dMaterial        BackgroundMaterial;
  28.     D3dExecuteData            d3dExData;
  29.     D3dRect                    clearrect = new D3dRect();
  30.     
  31.     boolean                    bScreenDirty;
  32.     int                        model;
  33.     int                        hardware;
  34.  
  35.     float D[] = new float[16];
  36.  
  37.     //////////////////////////////////////////////////////////////////////////
  38.     public void paint(Graphics g)
  39.     {
  40.         bScreenDirty = true;
  41.     }
  42.  
  43.     //////////////////////////////////////////////////////////////////////////
  44.     public Dimension minimumSize() 
  45.     {
  46.         return new Dimension(256,256);
  47.     }
  48.  
  49.     //////////////////////////////////////////////////////////////////////////
  50.     public Dimension maximumSize() 
  51.     {
  52.         return new Dimension(256,256);
  53.     }
  54.  
  55.     //////////////////////////////////////////////////////////////////////////
  56.     public Dimension preferredSize() 
  57.     {
  58.         return new Dimension(256,256);
  59.     }
  60.  
  61.     //////////////////////////////////////////////////////////////////////////
  62.     public FlipCubeCanvas()
  63.     {
  64.         super();
  65.  
  66.         src        = new Rect();
  67.         dst           = new Rect();
  68.  
  69.         dst.left   = 0;
  70.         dst.top    = 0;
  71.         dst.right  = 256;
  72.         dst.bottom = 256;
  73.  
  74.         src.left   = 0;
  75.         src.top    = 0;
  76.         src.right  = 256;
  77.         src.bottom = 256;
  78.     }
  79.  
  80.     int NumVertices = 24;
  81.     D3dLVertex CubeVertices[] = {
  82.         new D3dLVertex (-1.0f, 1.0f, 1.0f, 0x0000ff00, 0, 0.0f, 0.0f),
  83.         new D3dLVertex ( 1.0f, 1.0f, 1.0f, 0x0000ff00, 0, 0.0f, 0.0f),
  84.         new D3dLVertex ( 1.0f, 1.0f,-1.0f, 0x0000ff00, 0, 0.0f, 0.0f),
  85.         new D3dLVertex (-1.0f, 1.0f,-1.0f, 0x0000ff00, 0, 0.0f, 0.0f),
  86.  
  87.         new D3dLVertex ( 1.0f, 1.0f,-1.0f, 0x000000ff, 0, 0.0f, 0.0f),
  88.         new D3dLVertex ( 1.0f,-1.0f,-1.0f, 0x000000ff, 0, 0.0f, 0.0f),
  89.         new D3dLVertex (-1.0f,-1.0f,-1.0f, 0x000000ff, 0, 0.0f, 0.0f),
  90.         new D3dLVertex (-1.0f, 1.0f,-1.0f, 0x000000ff, 0, 0.0f, 0.0f),
  91.                
  92.         new D3dLVertex ( 1.0f, 1.0f, 1.0f, 0x00ff0000, 0, 0.0f, 0.0f),
  93.         new D3dLVertex ( 1.0f,-1.0f, 1.0f, 0x00ff0000, 0, 0.0f, 0.0f),
  94.         new D3dLVertex ( 1.0f,-1.0f,-1.0f, 0x00ff0000, 0, 0.0f, 0.0f),
  95.         new D3dLVertex ( 1.0f, 1.0f,-1.0f, 0x00ff0000, 0, 0.0f, 0.0f),
  96.  
  97.         new D3dLVertex (-1.0f, 1.0f, 1.0f, 0x00ff00ff, 0, 0.0f, 0.0f),
  98.         new D3dLVertex (-1.0f,-1.0f, 1.0f, 0x00ff00ff, 0, 0.0f, 0.0f),
  99.         new D3dLVertex ( 1.0f,-1.0f, 1.0f, 0x00ff00ff, 0, 0.0f, 0.0f),
  100.         new D3dLVertex ( 1.0f, 1.0f, 1.0f, 0x00ff00ff, 0, 0.0f, 0.0f),
  101.  
  102.         new D3dLVertex (-1.0f,-1.0f,-1.0f, 0x0000ffff, 0, 0.0f, 0.0f),
  103.         new D3dLVertex (-1.0f,-1.0f, 1.0f, 0x0000ffff, 0, 0.0f, 0.0f),
  104.         new D3dLVertex (-1.0f, 1.0f, 1.0f, 0x0000ffff, 0, 0.0f, 0.0f),
  105.         new D3dLVertex (-1.0f, 1.0f,-1.0f, 0x0000ffff, 0, 0.0f, 0.0f),
  106.   
  107.         new D3dLVertex ( 1.0f,-1.0f,-1.0f, 0x00ffff00, 0, 0.0f, 0.0f),
  108.         new D3dLVertex ( 1.0f,-1.0f, 1.0f, 0x00ffff00, 0, 0.0f, 0.0f),
  109.         new D3dLVertex (-1.0f,-1.0f, 1.0f, 0x00ffff00, 0, 0.0f, 0.0f),
  110.         new D3dLVertex (-1.0f,-1.0f,-1.0f, 0x00ffff00, 0, 0.0f, 0.0f)
  111.     };
  112.  
  113.     //
  114.     // Defines an array of triangle objects
  115.     //
  116.     int NumTri = 12;
  117.     D3dTriangle CubeTri[] = {
  118.         new D3dTriangle ((short)0,  (short)1,  (short)2,  D3DTRIFLAG_EDGEENABLETRIANGLE),
  119.         new D3dTriangle ((short)0,  (short)2,  (short)3,  D3DTRIFLAG_EDGEENABLETRIANGLE),
  120.            new D3dTriangle ((short)4,  (short)5,  (short)6,  D3DTRIFLAG_EDGEENABLETRIANGLE),  
  121.         new D3dTriangle ((short)4,  (short)6,  (short)7,  D3DTRIFLAG_EDGEENABLETRIANGLE),
  122.            new D3dTriangle ((short)8,  (short)9,  (short)10, D3DTRIFLAG_EDGEENABLETRIANGLE), 
  123.         new D3dTriangle ((short)8,  (short)10, (short)11, D3DTRIFLAG_EDGEENABLETRIANGLE),
  124.           new D3dTriangle ((short)12, (short)13, (short)14, D3DTRIFLAG_EDGEENABLETRIANGLE), 
  125.         new D3dTriangle ((short)12, (short)14, (short)15, D3DTRIFLAG_EDGEENABLETRIANGLE),
  126.           new D3dTriangle ((short)16, (short)17, (short)18, D3DTRIFLAG_EDGEENABLETRIANGLE),
  127.         new D3dTriangle ((short)16, (short)18, (short)19, D3DTRIFLAG_EDGEENABLETRIANGLE),
  128.           new D3dTriangle ((short)20, (short)21, (short)22, D3DTRIFLAG_EDGEENABLETRIANGLE),
  129.         new D3dTriangle ((short)20, (short)22, (short)23, D3DTRIFLAG_EDGEENABLETRIANGLE),
  130.     };
  131.  
  132.     int ViewHandle;
  133.     int ProjHandle;
  134.     int WorldHandle;
  135.  
  136.     float World[] = { 1.0f, 0.0f, 0.0f, 0.0f, 
  137.                                        0.0f, 1.0f, 0.0f, 0.0f, 
  138.                                      0.0f, 0.0f, 1.0f, 0.0f, 
  139.                                      0.0f, 0.0f, 0.0f, 1.0f };
  140.  
  141.     float Proj[] = { 2.0f, 0.0f, 0.0f, 0.0f, 
  142.                                      0.0f, 2.0f, 0.0f, 0.0f, 
  143.                                      0.0f, 0.0f, 1.0f, 1.0f, 
  144.                                      0.0f, 0.0f,-1.0f, 0.0f };
  145.  
  146.     float View[] = { 1.0f, 0.0f, 0.0f,  0.0f, 
  147.                                      0.0f, 1.0f, 0.0f,  0.0f, 
  148.                                      0.0f, 0.0f, 1.0f,  0.0f, 
  149.                                      0.0f, 0.0f, 10.0f, 1.0f };
  150.  
  151.     float Spin[] = { 0.994139f, -0.0283309f, 0.104333f, 0.0f, 
  152.                      0.0348344f, 0.997527f, -0.0610485f,0.0f, 
  153.                     -0.102346f,  0.0643251f, 0.992667f, 0.0f, 
  154.                      0.0f,       0.0f,       0.0f,      1.0f };
  155.  
  156.     void InitDDraw()
  157.     {
  158.         Guid g;
  159.  
  160.         //
  161.         // Create a DirectDraw object
  162.         //
  163.         DD = new DirectDraw();
  164.  
  165.         //
  166.         // The level is used to direct Direct Draw what 
  167.         // window behaviors are allowed, this is used
  168.         // primarly to allow Direct Draw applications
  169.         // to take over the whole screen.  Since 
  170.         // we are security bound in Java this value
  171.         // must allways be set to DDSCL_NORMAL
  172.         //
  173.         DD.setCooperativeLevel(this, DDSCL_NORMAL);
  174.  
  175.         //
  176.         // Create the primary surface
  177.         //
  178.         DDSurfaceDesc ddsd = new DDSurfaceDesc();
  179.         ddsd.flags = DDSD_CAPS;
  180.         ddsd.ddsCaps = DDSCAPS_PRIMARYSURFACE;
  181.         DDSPrimary = DD.createSurface(ddsd);
  182.  
  183.         //
  184.         // Create and Set the clipper for the primary surface
  185.         //
  186.         DDClipper = DD.createClipper(0);
  187.         // since we are not using any other components just clip to 
  188.         // the windows, and offest it from the upper left hand 
  189.         // cornet of the display
  190.         DDClipper.setComponent(DDCOOPERATIVE_OFFSETTOCOMPONENT,this);    
  191.         DDSPrimary.setClipper( DDClipper );
  192.  
  193.         //
  194.         // Create a blank palette, then fill it with 
  195.         // system palette color entries
  196.         //
  197.         DDPal = DD.createPalette(DDPCAPS_8BIT, (byte[])null);
  198.         if( DDPal != null)
  199.         {
  200.             // It will throw an execption if running in a 
  201.             // non-paletized video mode.
  202.                try
  203.                {
  204.                 DDSPrimary.setPalette(DDPal);
  205.                }
  206.                catch(Exception e)
  207.                {
  208.               }
  209.         }
  210.     }
  211.  
  212.     void InitD3D()
  213.     {
  214.         //
  215.         // Setup application info object
  216.         //
  217.         model = D3DCOLOR_RGB;
  218.  
  219.         // NOTE:
  220.         // If you have 3D hardware uncomment the next line and 
  221.         // make sure you uncomment the create surface line bellow
  222.         // to create the surface in hardware
  223.         //
  224. //        hardware = 1;
  225.         hardware = 0;
  226.  
  227.         //
  228.         // Create a Direct 3D Object 
  229.         //
  230.         D3D = DD.createD3D();
  231.  
  232.         //
  233.         // Create an offscreen rendering surface
  234.         //
  235.         DDSurfaceDesc ddsd = new DDSurfaceDesc();
  236.  
  237.  
  238.         ddsd.flags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  239. //        ddsd.ddsCaps = DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
  240.         ddsd.ddsCaps = DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
  241.         ddsd.width   = 256;    
  242.         ddsd.height  = 256; 
  243.         DDSOffscreen = DD.createSurface(ddsd);
  244.         if( DDPal != null)
  245.         {
  246.             try
  247.             {
  248.                 DDSOffscreen.setPalette(DDPal);
  249.                }
  250.                catch(Exception e)
  251.                {
  252.               }
  253.         }
  254.  
  255.         //
  256.         // Clear the surface
  257.         //
  258.            DDSOffscreen.bltColorFill(dst, 0);
  259.  
  260.         //
  261.         // Find the Device's Guid for a device type specified by 
  262.         // application info object
  263.         //
  264.         _Guid g = D3D.findDeviceForColorModel(model, hardware );
  265.  
  266.         // 
  267.         // Create the D3D Device object associated 
  268.         // with the offscreen surface
  269.         //
  270.         D3DDevice = DDSOffscreen.create3DDevice(g);
  271.  
  272.         //
  273.         // Create the D3D viewport object and 
  274.         // attach it to the device
  275.         //
  276.         D3DView = D3D.createViewport();    
  277.         D3DDevice.addViewport(D3DView);
  278.  
  279.         //
  280.         // Descirbe to Veiwport parameters and 
  281.         // set it into the Viewport
  282.         //
  283.  
  284.         D3dViewportDesc viewData = new D3dViewportDesc();
  285.         viewData.x        = 0;
  286.         viewData.y        = 0;
  287.         viewData.width    = 256;
  288.         viewData.height    = 256;
  289.         viewData.scaleX    = viewData.width  / 2.0f;
  290.         viewData.scaleY    = viewData.height / 2.0f;
  291.         viewData.maxX   = viewData.width  / (2.0f * viewData.scaleX);
  292.         viewData.maxY   = viewData.height / (2.0f * viewData.scaleY);
  293.         viewData.minZ    = 0.0f;    
  294.         viewData.maxZ    = 0.0f;
  295.  
  296.         D3DView.setViewport(viewData);
  297.  
  298.         //
  299.         // Set a background Material 
  300.         //
  301.         BackgroundMaterial = D3D.createMaterial();
  302.         D3dMaterialDesc MaterialDesc = new D3dMaterialDesc();
  303.         MaterialDesc.rampSize = 1;
  304.         BackgroundMaterial.setMaterial(MaterialDesc);
  305.  
  306.  
  307.         int BackgroundHandle; 
  308.         BackgroundHandle = BackgroundMaterial.getHandle(D3DDevice);
  309.         D3DView.setBackground(BackgroundHandle);
  310.     }
  311.  
  312.     void InitExecuteBuffer()
  313.     {                       
  314.         int bufferindex;
  315.         int vertexoffset;
  316.         int instructionoffset;
  317.  
  318.         D3dExecuteBufferDesc debDesc = new D3dExecuteBufferDesc();
  319.         debDesc.flags = D3DDEB_BUFSIZE;
  320.         debDesc.bufferSize = 64000;    
  321.  
  322.         D3DExBuf = D3DDevice.createExecuteBuffer(debDesc);
  323.         d3dExData = new D3dExecuteData();
  324.  
  325.         ViewHandle  = D3DDevice.createMatrix();
  326.         ProjHandle  = D3DDevice.createMatrix();
  327.         WorldHandle = D3DDevice.createMatrix();
  328.  
  329.         D3DDevice.setMatrix(ViewHandle,  View);
  330.         D3DDevice.setMatrix(ProjHandle,  Proj);
  331.         D3DDevice.setMatrix(WorldHandle, World);
  332.  
  333.         D3DExBuf.lock(debDesc);
  334.                                         
  335.         bufferindex = 0;
  336.          bufferindex += D3DExBuf.stateTransForm(3,bufferindex);
  337.         bufferindex += D3DExBuf.stateTransFormData(D3DTRANSFORMSTATE_PROJECTION,  ProjHandle,  bufferindex);
  338.         bufferindex += D3DExBuf.stateTransFormData(D3DTRANSFORMSTATE_VIEW,          ViewHandle,  bufferindex);
  339.         bufferindex += D3DExBuf.stateTransFormData(D3DTRANSFORMSTATE_WORLD,       WorldHandle, bufferindex);
  340.         bufferindex += D3DExBuf.stateRender(1,bufferindex);
  341.         bufferindex += D3DExBuf.stateRenderData(D3DRENDERSTATE_SHADEMODE, D3DSHADE_FLAT,bufferindex);
  342.         bufferindex += D3DExBuf.exit(bufferindex);
  343.  
  344.         D3DExBuf.unlock();
  345.  
  346.         d3dExData.vertexOffset      = 0;
  347.         d3dExData.vertexCount       = 0;
  348.         d3dExData.instructionOffset = 0;
  349.         d3dExData.instructionLength = bufferindex*4;
  350.         D3DExBuf.setExecuteData(d3dExData);
  351.  
  352.         D3DDevice.beginScene();
  353.         D3DDevice.execute(D3DExBuf,D3DView,D3DEXECUTE_UNCLIPPED);
  354.         D3DDevice.endScene();
  355.  
  356.         D3DExBuf.lock(debDesc);
  357.  
  358.         bufferindex = 0;
  359.         vertexoffset = bufferindex;    
  360.         for ( int i = 0; i < NumVertices; i++)
  361.             bufferindex += D3DExBuf.addLVertexData(CubeVertices[i], bufferindex);
  362.  
  363.         instructionoffset = bufferindex;
  364.  
  365.         bufferindex += D3DExBuf.setStatus(D3DSETSTATUS_ALL, D3DSTATUS_DEFAULT, 256, 256, 0, 0, bufferindex);
  366.  
  367.          bufferindex += D3DExBuf.stateRender(2,bufferindex);
  368.         bufferindex += D3DExBuf.stateRenderData(D3DRENDERSTATE_FILLMODE,  D3DFILL_SOLID, bufferindex);
  369.         bufferindex += D3DExBuf.stateRenderData(D3DRENDERSTATE_SHADEMODE, D3DSHADE_FLAT, bufferindex);
  370.  
  371.         bufferindex += D3DExBuf.processVertices(6,bufferindex);
  372.         for ( int i = 0; i < 6; i++) 
  373.           bufferindex += D3DExBuf.processVerticesData(D3DPROCESSVERTICES_TRANSFORM, i*4, i*4, 4, bufferindex);
  374.  
  375.         bufferindex += D3DExBuf.triangleList(NumTri, bufferindex);
  376.         for ( int i = 0; i < NumTri; i++) 
  377.            bufferindex += D3DExBuf.triangleListData(CubeTri[i], bufferindex);
  378.  
  379.         bufferindex += D3DExBuf.exit(bufferindex);
  380.  
  381.         D3DExBuf.unlock();
  382.  
  383.         d3dExData.vertexOffset      = vertexoffset*4;
  384.         d3dExData.vertexCount       = NumVertices;
  385.         d3dExData.instructionOffset = instructionoffset*4;
  386.         d3dExData.instructionLength = (bufferindex - instructionoffset)*4;
  387.  
  388.         D3DExBuf.setExecuteData(d3dExData);
  389.     }
  390.  
  391.     //////////////////////////////////////////////////////////////////////////
  392.     public void updateframe()
  393.     {
  394.         try 
  395.         {
  396.             //
  397.             // Set the new rotation matrix
  398.             //
  399.             MultiplyD3dMatrix(World, Spin, World);
  400.             D3DDevice.setMatrix(WorldHandle, World);
  401.             
  402.             //
  403.             // Render the object 
  404.             // 
  405.             D3DDevice.beginScene();
  406.             D3DDevice.execute(D3DExBuf,D3DView,D3DEXECUTE_CLIPPED);
  407.             D3DDevice.endScene();
  408.  
  409.             //
  410.             // Retreive how much of the screen is dirty
  411.             //
  412.             D3DExBuf.getExecuteData(d3dExData);
  413.  
  414.             //
  415.             // Determine how much of the screen has
  416.             // been invaidated by the render or a repaint
  417.             //
  418.             if ( bScreenDirty == false )
  419.             {
  420.                 //
  421.                 // Check if the last src rect has
  422.                 // grown any larger than the 
  423.                 // extents returned by Direct3d
  424.                 //
  425.                 if ( src.left > d3dExData.x1 )
  426.                     src.left   = d3dExData.x1;
  427.                 if ( src.top > d3dExData.y1 )
  428.                     src.top    = d3dExData.y1;
  429.                 if ( src.right < d3dExData.x2 )
  430.                     src.right  = d3dExData.x2;
  431.                 if ( src.bottom < d3dExData.y2 )
  432.                     src.bottom = d3dExData.y2;
  433.             }
  434.             else
  435.             {
  436.                 src.left   = 0;
  437.                 src.top    = 0;
  438.                 src.right  = 255;
  439.                 src.bottom = 255;
  440.             }
  441.  
  442.             dst.left   = src.left;
  443.             dst.top    = src.top;
  444.             dst.right  = src.right;
  445.             dst.bottom = src.bottom;
  446.  
  447.             //
  448.             // Blit the Offscreen surface attached to
  449.             // the Viewport to the screen 
  450.             // 
  451.                DDSPrimary.blt(dst, DDSOffscreen, src, DDBLT_WAIT);
  452.  
  453.             //
  454.             // Blt has been complete this will be set to true
  455.             // only during init or when a paint is nessary 
  456.             //
  457.             bScreenDirty = false;        
  458.  
  459.             //
  460.             // Clear the viewport
  461.             //
  462.             clearrect.x1 = d3dExData.x1;
  463.             clearrect.y1 = d3dExData.y1;
  464.             clearrect.x2 = d3dExData.x2;
  465.             clearrect.y2 = d3dExData.y2;
  466.  
  467.             D3DView.clear( clearrect, D3DCLEAR_TARGET);
  468.         } 
  469.         catch (java.awt.IllegalComponentStateException e) {
  470.             // this can happen when the applet is being shutdown and the windows
  471.             // has been destroyed, yet my thread may be still running.
  472.         }
  473.         catch(Exception e) 
  474.         {
  475.             System.out.println("D3D Rendering Exception: "+e);
  476.         }
  477.     }
  478.  
  479.     //////////////////////////////////////////////////////////////////////////
  480.     void MultiplyD3dMatrix(float Dst[], float Src1[], float Src2[])
  481.     {
  482.         int i, r, c;
  483.  
  484.         for (r = 0; r < 4; r++) 
  485.         {
  486.             for (c = 0; c < 4; c++) 
  487.             {
  488.                 D[(r*4)+c] = 0.0f;
  489.                 for (i = 0; i < 4; i++)
  490.                     D[(r*4)+c] += Src1[(r*4)+i] * Src2[(i*4)+c];
  491.             }
  492.         }
  493.  
  494.         for (i = 0; i < 16; i++ )
  495.             Dst[i] = D[i];
  496.     }
  497. }