Project

General

Profile

« Previous | Next » 

Revision a3a05347

Added by Leszek Koltunski over 4 years ago

Improve setTextureMap.

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.java
26 26
import org.distorted.library.main.DistortedLibrary;
27 27
import org.distorted.library.main.InternalBuffer;
28 28
import org.distorted.library.program.DistortedProgram;
29
import org.distorted.library.type.Static4D;
29 30

  
30 31
import java.util.ArrayList;
31 32

  
......
71 72
   private class Component
72 73
     {
73 74
     private int mEndIndex;
74
     private float[] mTextureMap;
75
     private Static4D mTextureMap;
75 76

  
76 77
     Component(int end)
77 78
       {
78
       mEndIndex = end;
79

  
80
       mTextureMap    = new float[4];
81
       mTextureMap[0] = 0.0f;  // LowerLeft_X
82
       mTextureMap[1] = 0.0f;  // LowerLeft_Y
83
       mTextureMap[2] = 1.0f;  // Width
84
       mTextureMap[3] = 1.0f;  // Height
79
       mEndIndex  = end;
80
       mTextureMap= new Static4D(0,0,1,1);
85 81
       }
86 82
     Component(Component original)
87 83
       {
88 84
       mEndIndex = original.mEndIndex;
89
       mTextureMap = new float[4];
90
       System.arraycopy(original.mTextureMap,0,mTextureMap,0,4);
85

  
86
       float x = original.mTextureMap.get0();
87
       float y = original.mTextureMap.get1();
88
       float z = original.mTextureMap.get2();
89
       float w = original.mTextureMap.get3();
90
       mTextureMap = new Static4D(x,y,z,w);
91 91
       }
92 92

  
93
     void setMap(float[] newMap)
93
     void setMap(Static4D map)
94 94
       {
95
       System.arraycopy(newMap,0,mTextureMap,0,4);
95
       mTextureMap.set(map.get0(),map.get1(),map.get2(),map.get3());
96 96
       }
97 97
     }
98 98

  
......
452 452

  
453 453
///////////////////////////////////////////////////////////////////////////////////////////////////
454 454
/**
455
 * Sets texture maps for all components of this mesh.
455
 * Sets texture maps for (some of) the components of this mesh.
456 456
 * <p>
457
 * Please note that calling this once with the complete list of Maps will be much faster than
458
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
459
 * each time.
460
 * 'maps' needs to be maps[NumComponentsInThisMesh][4]. [0] is the lower-left corner's X, [1]- its Y,
461
 * [2] - width, [3] - height of the map.
462
 * For example map[0] = new float { 0.0, 0.5, 0.5, 0.5 } sets the 0th component texture map to the
457
 * Format: ( x of lower-left corner, y of lower-left corner, width, height ).
458
 * For example maps[0] = new Static4D( 0.0, 0.5, 0.5, 0.5 ) sets the 0th component texture map to the
463 459
 * upper-left quadrant of the texture.
460
 * <p>
461
 * Probably the most common user case would be sending as many maps as there are components in this
462
 * Mesh. One can also send less, or more (the extraneous ones will be ignored) and set some of them
463
 * to null (those will be ignored as well). So if there are 5 components, and we want to set the map
464
 * of the 2nd and 4rd one, call this with
465
 * maps = new Static4D[4]
466
 * maps[0] = null
467
 * maps[1] = the map for the 2nd component
468
 * maps[2] = null
469
 * maps[3] = the map for the 4th component
470
 *
471
 * A map's width and height have to be non-zero (but can be negative!)
464 472
 */
465
   public void setTextureMap(float[][] maps)
473
   public void setTextureMap(Static4D[] maps)
466 474
     {
467 475
     int num_comp = mComponent.size();
468 476
     int num_maps = maps.length;
469 477
     int min = num_comp<num_maps ? num_comp : num_maps;
470 478
     int vertex = 0;
471 479
     int index  = TEX_ATTRIB;
472
     float[] newMap, oldMap;
480
     Static4D newMap, oldMap;
473 481
     Component comp;
482
     float newW, newH, ratW, ratH, movX, movY;
474 483

  
475 484
     for(int i=0; i<min; i++)
476 485
       {
477
       if( maps[i]!=null && maps[i][2]!=0.0f && maps[i][3]!=0.0f )
486
       newMap = maps[i];
487

  
488
       if( newMap!=null )
478 489
         {
479
         comp = mComponent.get(i);
480
         newMap = maps[i];
481
         oldMap = comp.mTextureMap;
490
         newW = newMap.get2();
491
         newH = newMap.get3();
482 492

  
483
         for( ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
493
         if( newW!=0.0f && newH!=0.0f )
484 494
           {
485
           mVertAttribs[index  ] = (newMap[2]/oldMap[2])*(mVertAttribs[index  ]-oldMap[0]) + newMap[0];
486
           mVertAttribs[index+1] = (newMap[3]/oldMap[3])*(mVertAttribs[index+1]-oldMap[1]) + newMap[1];
495
           comp = mComponent.get(i);
496
           oldMap = comp.mTextureMap;
497
           ratW = newW/oldMap.get2();
498
           ratH = newH/oldMap.get3();
499
           movX = newMap.get0() - ratW*oldMap.get0();
500
           movY = newMap.get1() - ratH*oldMap.get1();
501

  
502
           for( ; vertex<=comp.mEndIndex; vertex++, index+=VERT_ATTRIBS)
503
             {
504
             mVertAttribs[index  ] = ratW*mVertAttribs[index  ] + movX;
505
             mVertAttribs[index+1] = ratH*mVertAttribs[index+1] + movY;
506
             }
507
           comp.setMap(newMap);
487 508
           }
488
         comp.setMap(newMap);
489 509
         }
490 510
       }
491 511

  

Also available in: Unified diff