Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshFlat.java @ 7a5e538a

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

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

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

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

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

    
55
     remainingVert = numVertices;
56
     }
57

    
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

    
73
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = x-0.5f;
74
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.5f-y;
75
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;
76

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

    
80
     return vertex+1;
81
     }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

    
89
     if( vertex>0 )
90
       {
91
       remainingVert--;
92

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

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

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

    
105
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
106
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
107

    
108
       vertex++;
109
       }
110

    
111
     return vertex;
112
     }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

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

    
126
     //android.util.Log.d("MeshFlat", "buildGrid");
127

    
128
     y = 0.0f;
129

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

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

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

    
149
         lastBlockIsNE = currentBlockIsNE;
150
         x+=X;
151
         }
152

    
153
       y+=Y;
154
       }
155

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

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

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

    
171
     return ret;
172
     }
173
*/
174

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

    
189
    float[] attribs= new float[VERT_ATTRIBS*numVertices];
190

    
191
    buildGrid(attribs);
192

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

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

    
199
    setAttribs(attribs);
200
    }
201
 }
(3-3/3)