Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshFlat.java @ a51fe521

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;
21

    
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24

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

    
37
  private static float[] mBoundingVert = new float[] { -0.5f,+0.5f,0.0f,
38
                                                       -0.5f,-0.5f,0.0f,
39
                                                       +0.5f,+0.5f,0.0f,
40
                                                       +0.5f,-0.5f,0.0f };
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
// Create a flat, full grid.
44

    
45
  private void computeNumberOfVertices(int cols, int rows)
46
     {
47
     mRows=rows;
48
     mCols=cols;
49

    
50
     if( cols==1 && rows==1 )
51
       {
52
       numVertices = 4;
53
       }
54
     else
55
       {
56
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
57
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
58
       }
59

    
60
     //android.util.Log.e("BITMAP","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
61

    
62
     remainingVert = numVertices;
63
     }
64

    
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  private int addVertex(int vertex, float x, float y, float[] attribs)
69
     {
70
     remainingVert--;
71

    
72
     attribs[8*vertex  ] = x-0.5f;
73
     attribs[8*vertex+1] = 0.5f-y;
74
     attribs[8*vertex+2] = 0;
75

    
76
     attribs[8*vertex+3] = 0.0f;
77
     attribs[8*vertex+4] = 0.0f;
78
     attribs[8*vertex+5] = 1.0f;
79

    
80
     attribs[8*vertex+6] = x;
81
     attribs[8*vertex+7] = 1.0f-y;
82

    
83
     return vertex+1;
84
     }
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private int repeatLast(int vertex, float[] attribs)
88
     {
89
     remainingVert--;
90

    
91
     //android.util.Log.e("BITMAP", "repeating last vertex!");
92

    
93
     if( vertex>0 )
94
       {
95
       attribs[8*vertex  ] = attribs[8*vertex-8];
96
       attribs[8*vertex+1] = attribs[8*vertex-7];
97
       attribs[8*vertex+2] = attribs[8*vertex-6];
98
       attribs[8*vertex+3] = attribs[8*vertex-5];
99
       attribs[8*vertex+4] = attribs[8*vertex-4];
100
       attribs[8*vertex+5] = attribs[8*vertex-3];
101
       attribs[8*vertex+6] = attribs[8*vertex-2];
102
       attribs[8*vertex+7] = attribs[8*vertex-1];
103

    
104
       vertex++;
105
       }
106

    
107
     return vertex;
108
     }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  private void buildGrid(float[] attribs)
113
     {
114
     boolean lastBlockIsNE = false;
115
     boolean currentBlockIsNE;
116
     int vertex = 0;
117

    
118
     float x,y;
119
     final float X = 1.0f/mCols;
120
     final float Y = 1.0f/mRows;
121

    
122
     //android.util.Log.d("BITMAP", "buildGrid");
123

    
124
     y = 0.0f;
125

    
126
     for(int row=0; row<mRows; row++)
127
       {
128
       x = 0.0f;
129

    
130
       for(int col=0; col<mCols; col++)
131
         {
132
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
133

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

    
145
         lastBlockIsNE = currentBlockIsNE;
146
         x+=X;
147
         }
148

    
149
       y+=Y;
150
       }
151

    
152
     //android.util.Log.d("BITMAP", "buildGrid done");
153
     }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
/*
157
  private static String debug(float[] val, int stop)
158
     {
159
     String ret="";
160

    
161
     for(int i=0; i<val.length; i++)
162
        {
163
        if( i%stop==0 ) ret+="\n";
164
        ret+=(" "+val[i]);
165
        }
166

    
167
     return ret;
168
     }
169
*/
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  float[] getBoundingVertices()
173
     {
174
     return mBoundingVert;
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[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
192

    
193
    buildGrid(attribs);
194

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

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

    
201
    mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
202
    mVertAttribs.put(attribs).position(0);
203
    }
204
 }
(25-25/26)