Project

General

Profile

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

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

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 e0b6c593 Leszek Koltunski
/**
27 05403bba Leszek Koltunski
 * Create a flat, rectangular grid.
28 e0b6c593 Leszek Koltunski
 * <p>
29
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
30 26df012c Leszek Koltunski
 * effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
31 e0b6c593 Leszek Koltunski
 */
32 26df012c Leszek Koltunski
public class MeshFlat extends MeshObject
33 6a06a912 Leszek Koltunski
  {
34 adb2661c Leszek Koltunski
  private int mCols, mRows;
35
  private int remainingVert;
36 6a06a912 Leszek Koltunski
37 7d755851 Leszek Koltunski
  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 69ed1eb4 Leszek Koltunski
42 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
43 cacc63de Leszek Koltunski
// Create a flat, full grid.
44 6a06a912 Leszek Koltunski
45 a51fe521 Leszek Koltunski
  private void computeNumberOfVertices(int cols, int rows)
46 adb2661c Leszek Koltunski
     {
47 79f172ab Leszek Koltunski
     mRows=rows;
48
     mCols=cols;
49 d44ac567 Leszek Koltunski
50 79f172ab Leszek Koltunski
     if( cols==1 && rows==1 )
51
       {
52 a51fe521 Leszek Koltunski
       numVertices = 4;
53 79f172ab Leszek Koltunski
       }
54
     else
55 adb2661c Leszek Koltunski
       {
56 a51fe521 Leszek Koltunski
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
57
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
58 79f172ab Leszek Koltunski
       }
59 d44ac567 Leszek Koltunski
60 a51fe521 Leszek Koltunski
     //android.util.Log.e("BITMAP","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
61 d44ac567 Leszek Koltunski
62 a51fe521 Leszek Koltunski
     remainingVert = numVertices;
63 adb2661c Leszek Koltunski
     }
64 d44ac567 Leszek Koltunski
65 5bf698ee Leszek Koltunski
66 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67 6a06a912 Leszek Koltunski
68 a51fe521 Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs)
69 adb2661c Leszek Koltunski
     {
70
     remainingVert--;
71 6a06a912 Leszek Koltunski
72 a51fe521 Leszek Koltunski
     attribs[8*vertex  ] = x-0.5f;
73
     attribs[8*vertex+1] = 0.5f-y;
74
     attribs[8*vertex+2] = 0;
75 6a06a912 Leszek Koltunski
76 a51fe521 Leszek Koltunski
     attribs[8*vertex+3] = 0.0f;
77
     attribs[8*vertex+4] = 0.0f;
78
     attribs[8*vertex+5] = 1.0f;
79 6a06a912 Leszek Koltunski
80 a51fe521 Leszek Koltunski
     attribs[8*vertex+6] = x;
81
     attribs[8*vertex+7] = 1.0f-y;
82 6a06a912 Leszek Koltunski
83 adb2661c Leszek Koltunski
     return vertex+1;
84
     }
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86 d44ac567 Leszek Koltunski
87 a51fe521 Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs)
88 adb2661c Leszek Koltunski
     {
89
     remainingVert--;
90 6a06a912 Leszek Koltunski
91 adb2661c Leszek Koltunski
     //android.util.Log.e("BITMAP", "repeating last vertex!");
92 6a06a912 Leszek Koltunski
93 adb2661c Leszek Koltunski
     if( vertex>0 )
94
       {
95 a51fe521 Leszek Koltunski
       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 d44ac567 Leszek Koltunski
104 adb2661c Leszek Koltunski
       vertex++;
105
       }
106 6a06a912 Leszek Koltunski
107 adb2661c Leszek Koltunski
     return vertex;
108
     }
109 d44ac567 Leszek Koltunski
110 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 a51fe521 Leszek Koltunski
  private void buildGrid(float[] attribs)
113 adb2661c Leszek Koltunski
     {
114
     boolean lastBlockIsNE = false;
115
     boolean currentBlockIsNE;
116
     int vertex = 0;
117
118 69ed1eb4 Leszek Koltunski
     float x,y;
119
     final float X = 1.0f/mCols;
120
     final float Y = 1.0f/mRows;
121
122 adb2661c Leszek Koltunski
     //android.util.Log.d("BITMAP", "buildGrid");
123
124 69ed1eb4 Leszek Koltunski
     y = 0.0f;
125
126 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
127
       {
128 69ed1eb4 Leszek Koltunski
       x = 0.0f;
129
130 adb2661c Leszek Koltunski
       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 a51fe521 Leszek Koltunski
           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 adb2661c Leszek Koltunski
           }
142 a51fe521 Leszek Koltunski
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
143
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
144 adb2661c Leszek Koltunski
145
         lastBlockIsNE = currentBlockIsNE;
146 69ed1eb4 Leszek Koltunski
         x+=X;
147 adb2661c Leszek Koltunski
         }
148 69ed1eb4 Leszek Koltunski
149
       y+=Y;
150 adb2661c Leszek Koltunski
       }
151
152
     //android.util.Log.d("BITMAP", "buildGrid done");
153
     }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156 79f172ab Leszek Koltunski
/*
157 a51fe521 Leszek Koltunski
  private static String debug(float[] val, int stop)
158 79f172ab Leszek Koltunski
     {
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 11fb6ce0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 a51fe521 Leszek Koltunski
  float[] getBoundingVertices()
173 69ed1eb4 Leszek Koltunski
     {
174
     return mBoundingVert;
175
     }
176 11fb6ce0 Leszek Koltunski
177 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178 a56bc359 Leszek Koltunski
// PUBLIC API
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180 adb2661c Leszek Koltunski
/**
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 a51fe521 Leszek Koltunski
 public MeshFlat(int cols, int rows)
187
    {
188
    super(0.0f);
189
    computeNumberOfVertices(cols,rows);
190 adb2661c Leszek Koltunski
191 cab7c165 Leszek Koltunski
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
192 adb2661c Leszek Koltunski
193 a51fe521 Leszek Koltunski
    buildGrid(attribs);
194 adb2661c Leszek Koltunski
195 a51fe521 Leszek Koltunski
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
196
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
197 adb2661c Leszek Koltunski
198 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
199
      android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
200 adb2661c Leszek Koltunski
201 a51fe521 Leszek Koltunski
    mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
202
    mVertAttribs.put(attribs).position(0);
203
    }
204
 }