Project

General

Profile

Download (7.76 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshFlat.java @ a16bf106

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.mesh;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * Create a flat, rectangular grid.
25
 * <p>
26
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
27
 * effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
28
 */
29
public class MeshFlat extends MeshBase
30
  {
31
  private int mCols, mRows;
32
  private int remainingVert;
33
  private int numVertices;
34
  private float mInfCorr;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
// Create a flat, full grid.
38

    
39
  private void computeNumberOfVertices(int cols, int rows)
40
     {
41
     mRows=rows;
42
     mCols=cols;
43

    
44
     mInfCorr = mRows>mCols ? mRows:mCols;
45

    
46
     if( cols==1 && rows==1 )
47
       {
48
       numVertices = 4;
49
       }
50
     else
51
       {
52
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
53
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
54
       }
55

    
56
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
57

    
58
     remainingVert = numVertices;
59
     }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private int addVertex(int vertex, float x, float y, float[] attribs)
64
     {
65
     remainingVert--;
66

    
67
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
68
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
69
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
70

    
71
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
72
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
73
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
74

    
75
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = (x-0.5f)*mInfCorr;
76
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y)*mInfCorr;
77
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f   *mInfCorr;  // Inflated surface needs to be slightly in front
78

    
79
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
80
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
81

    
82
     return vertex+1;
83
     }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private int repeatLast(int vertex, float[] attribs)
88
     {
89
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
90

    
91
     if( vertex>0 )
92
       {
93
       remainingVert--;
94

    
95
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
96
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
97
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
98

    
99
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
100
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
101
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
102

    
103
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB  ];
104
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
105
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
106

    
107
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
108
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
109

    
110
       vertex++;
111
       }
112

    
113
     return vertex;
114
     }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  private void buildGrid(float[] attribs)
119
     {
120
     boolean lastBlockIsNE = false;
121
     boolean currentBlockIsNE;
122
     int vertex = 0;
123

    
124
     float x,y;
125
     final float X = 1.0f/mCols;
126
     final float Y = 1.0f/mRows;
127

    
128
     //android.util.Log.d("MeshFlat", "buildGrid");
129

    
130
     y = 0.0f;
131

    
132
     for(int row=0; row<mRows; row++)
133
       {
134
       x = 0.0f;
135

    
136
       for(int col=0; col<mCols; col++)
137
         {
138
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
139

    
140
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
141
           {
142
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
143
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
144
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
145
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
146
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
147
           }
148
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
149
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
150

    
151
         lastBlockIsNE = currentBlockIsNE;
152
         x+=X;
153
         }
154

    
155
       y+=Y;
156
       }
157

    
158
     //android.util.Log.d("MeshFlat", "buildGrid done");
159
     }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/*
163
  private static String debug(float[] val, int stop)
164
     {
165
     String ret="";
166

    
167
     for(int i=0; i<val.length; i++)
168
        {
169
        if( i%stop==0 ) ret+="\n";
170
        ret+=(" "+val[i]);
171
        }
172

    
173
     return ret;
174
     }
175
*/
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// PUBLIC API
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
/**
181
 * Creates the underlying grid of vertices, normals and texture coords.
182
 *
183
 * @param cols Number of columns in the grid.
184
 * @param rows Number of rows in the grid.
185
 */
186
 public MeshFlat(int cols, int rows)
187
    {
188
    super(0.0f);
189
    computeNumberOfVertices(cols,rows);
190

    
191
    float[] attribs= new float[VERT_ATTRIBS*numVertices];
192

    
193
    buildGrid(attribs);
194

    
195
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
196
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,VERT_ATTRIBS) );
197

    
198
    if( remainingVert!=0 )
199
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
200

    
201
    setAttribs(attribs);
202
    }
203
 }
(3-3/3)