Project

General

Profile

« Previous | Next » 

Revision eeb5d115

Added by Leszek Koltunski over 3 years ago

Progress with MeshPolygon.

View differences:

src/main/java/org/distorted/library/mesh/MeshPolygon.java
37 37
  private int remainingVert;
38 38
  private int numVertices;
39 39

  
40
  private float[] mBandQuot;
41

  
40 42
///////////////////////////////////////////////////////////////////////////////////////////////////
41 43
// polygonVertices>=3 , polygonBands>=2
42 44

  
......
48 50

  
49 51
///////////////////////////////////////////////////////////////////////////////////////////////////
50 52

  
51
  private int addVertex(int vertex, int polyBand, int polyVertex, int index, float[] attribs1, float[] attribs2)
53
  private void computeCache()
52 54
    {
53
    remainingVert--;
54

  
55
    if( polyBand<mNumPolygonBands-1 )
56
      {
57
      float band = mPolygonBands[2*polyBand  ];
58
      float elev = mPolygonBands[2*polyBand+1];
55
    mBandQuot = new float[mNumPolygonBands];
59 56

  
60
      float Xfirst  = mPolygonVertices[2*polyVertex  ];
61
      float Yfirst  = mPolygonVertices[2*polyVertex+1];
57
    int next, prev;
62 58

  
63
      int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
64

  
65
      float Xlast   = mPolygonVertices[2*polyEndVer  ];
66
      float Ylast   = mPolygonVertices[2*polyEndVer+1];
59
    for(int band=0; band<mNumPolygonBands; band++)
60
      {
61
      next = (band==mNumPolygonBands-1 ? band : band+1);
62
      prev = (band==                 0 ? band : band-1);
67 63

  
68
      float quot = (float)index / (mNumPolygonBands-1-polyBand);
64
      mBandQuot[band] = (mPolygonBands[2*prev+1]-mPolygonBands[2*next+1]) / (mPolygonBands[2*next]-mPolygonBands[2*prev]);
65
      }
66
    }
69 67

  
70
      float xEdge = Xfirst + quot*(Xlast-Xfirst);
71
      float yEdge = Yfirst + quot*(Ylast-Yfirst);
68
///////////////////////////////////////////////////////////////////////////////////////////////////
72 69

  
73
      float x = band*xEdge;
74
      float y = band*yEdge;
70
  private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, int index, float[] attribs1, float[] attribs2)
71
    {
72
    remainingVert--;
75 73

  
76
      int nextPolyBand = polyBand+1;
77
      int prevPolyBand = (polyBand==0 ? polyBand : polyBand-1);
74
    float quot = polyBand<mNumPolygonBands-1 ? (float)index / (mNumPolygonBands-1-polyBand) : 1.0f;
78 75

  
79
      float concentricStep = mPolygonBands[2*nextPolyBand]-mPolygonBands[2*prevPolyBand];
80
      float nx = concentricStep*xEdge;
81
      float ny = concentricStep*yEdge;
76
    float vx,vy,vz;
82 77

  
83
      float nz = mPolygonBands[2*prevPolyBand+1]-mPolygonBands[2*nextPolyBand+1];
84
      float lenXYZ = (float)Math.sqrt(nx*nx + ny*ny + nz*nz);
85
      float lenXY  = (float)Math.sqrt(nx*nx + ny*ny);
78
    float Xfirst= mPolygonVertices[2*polyVertex  ];
79
    float Yfirst= mPolygonVertices[2*polyVertex+1];
80
    float Xlast = mPolygonVertices[2*polyEndVer  ];
81
    float Ylast = mPolygonVertices[2*polyEndVer+1];
86 82

  
87
      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] =    x;
88
      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] =    y;
89
      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = elev;
83
    float xEdge = Xfirst + quot*(Xlast-Xfirst);
84
    float yEdge = Yfirst + quot*(Ylast-Yfirst);
90 85

  
91
      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = (nz*nx) / (lenXY*lenXYZ);
92
      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = (nz*ny) / (lenXY*lenXYZ);
93
      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = lenXY / lenXYZ;
86
    float x = mPolygonBands[2*polyBand]*xEdge;
87
    float y = mPolygonBands[2*polyBand]*yEdge;
94 88

  
95
      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
96
      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
89
    if( quot==0.0f || quot==1.0f )
90
      {
91
      vx = mBandQuot[polyBand]*xEdge;
92
      vy = mBandQuot[polyBand]*yEdge;
93
      vz = xEdge*xEdge + yEdge*yEdge;
97 94
      }
98 95
    else
99 96
      {
100
      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = 0.0f;
101
      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = 0.0f;
102
      attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = mPolygonBands[2*polyBand+1];
97
      vx = mBandQuot[polyBand]*(Ylast-Yfirst);
98
      vy = mBandQuot[polyBand]*(Xfirst-Xlast);
99
      float tmp = Xfirst*Ylast - Xlast*Yfirst;
100
      vz = (tmp<0 ? -tmp:tmp);
101
      }
103 102

  
104
      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
105
      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
106
      attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
103
    float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz);
107 104

  
108
      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = 0.5f;
109
      attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = 0.5f;
110
      }
105
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x;
106
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y;
107
    attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = mPolygonBands[2*polyBand+1];
108

  
109
    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = vx/len;
110
    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = vy/len;
111
    attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = vz/len;
112

  
113
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x+0.5f;
114
    attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f;
111 115

  
112 116
    return vertex+1;
113 117
    }
......
118 122
    {
119 123
    if( polyVertex==0 )
120 124
      {
121
      vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
125
      vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
122 126

  
123 127
      if( polyBand>0 )
124 128
        {
125
        vertex = addVertex(vertex,polyBand,polyVertex,0,attribs1,attribs2);
129
        vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2);
126 130
        }
127 131
      }
128 132

  
129 133
    int numPairs = mNumPolygonBands-1-polyBand;
134
    int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1;
130 135

  
131
    for(int pair=0; pair<numPairs; pair++)
136
    for(int index=0; index<numPairs; index++)
132 137
      {
133
      vertex = addVertex(vertex,polyBand+1,polyVertex,pair  ,attribs1,attribs2);
134
      vertex = addVertex(vertex,polyBand  ,polyVertex,pair+1,attribs1,attribs2);
138
      vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,index  ,attribs1,attribs2);
139
      vertex = addVertex(vertex,polyBand  ,polyVertex,polyEndVer,index+1,attribs1,attribs2);
135 140
      }
136 141

  
137 142
    return vertex;
......
172 177
    mNumPolygonBands      = mPolygonBands.length /2;
173 178

  
174 179
    computeNumberOfVertices(mNumPolygonVertices,mNumPolygonBands);
180
    computeCache();
175 181

  
176 182
    float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
177 183
    float[] attribs2= new float[VERT2_ATTRIBS*numVertices];

Also available in: Unified diff