Project

General

Profile

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

library / src / main / java / org / distorted / library / main / MeshFlat.java @ 466450b5

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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
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
///////////////////////////////////////////////////////////////////////////////////////////////////
38 cacc63de Leszek Koltunski
// Create a flat, full grid.
39 6a06a912 Leszek Koltunski
40 a51fe521 Leszek Koltunski
  private void computeNumberOfVertices(int cols, int rows)
41 adb2661c Leszek Koltunski
     {
42 79f172ab Leszek Koltunski
     mRows=rows;
43
     mCols=cols;
44 d44ac567 Leszek Koltunski
45 79f172ab Leszek Koltunski
     if( cols==1 && rows==1 )
46
       {
47 a51fe521 Leszek Koltunski
       numVertices = 4;
48 79f172ab Leszek Koltunski
       }
49
     else
50 adb2661c Leszek Koltunski
       {
51 a51fe521 Leszek Koltunski
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
52
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
53 79f172ab Leszek Koltunski
       }
54 d44ac567 Leszek Koltunski
55 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
56 d44ac567 Leszek Koltunski
57 a51fe521 Leszek Koltunski
     remainingVert = numVertices;
58 adb2661c Leszek Koltunski
     }
59 d44ac567 Leszek Koltunski
60 5bf698ee Leszek Koltunski
61 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62 6a06a912 Leszek Koltunski
63 a51fe521 Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs)
64 adb2661c Leszek Koltunski
     {
65
     remainingVert--;
66 6a06a912 Leszek Koltunski
67 a51fe521 Leszek Koltunski
     attribs[8*vertex  ] = x-0.5f;
68
     attribs[8*vertex+1] = 0.5f-y;
69
     attribs[8*vertex+2] = 0;
70 6a06a912 Leszek Koltunski
71 a51fe521 Leszek Koltunski
     attribs[8*vertex+3] = 0.0f;
72
     attribs[8*vertex+4] = 0.0f;
73
     attribs[8*vertex+5] = 1.0f;
74 6a06a912 Leszek Koltunski
75 a51fe521 Leszek Koltunski
     attribs[8*vertex+6] = x;
76
     attribs[8*vertex+7] = 1.0f-y;
77 6a06a912 Leszek Koltunski
78 adb2661c Leszek Koltunski
     return vertex+1;
79
     }
80 466450b5 Leszek Koltunski
81 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82 d44ac567 Leszek Koltunski
83 a51fe521 Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs)
84 adb2661c Leszek Koltunski
     {
85
     remainingVert--;
86 6a06a912 Leszek Koltunski
87 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
88 6a06a912 Leszek Koltunski
89 adb2661c Leszek Koltunski
     if( vertex>0 )
90
       {
91 a51fe521 Leszek Koltunski
       attribs[8*vertex  ] = attribs[8*vertex-8];
92
       attribs[8*vertex+1] = attribs[8*vertex-7];
93
       attribs[8*vertex+2] = attribs[8*vertex-6];
94
       attribs[8*vertex+3] = attribs[8*vertex-5];
95
       attribs[8*vertex+4] = attribs[8*vertex-4];
96
       attribs[8*vertex+5] = attribs[8*vertex-3];
97
       attribs[8*vertex+6] = attribs[8*vertex-2];
98
       attribs[8*vertex+7] = attribs[8*vertex-1];
99 d44ac567 Leszek Koltunski
100 adb2661c Leszek Koltunski
       vertex++;
101
       }
102 6a06a912 Leszek Koltunski
103 adb2661c Leszek Koltunski
     return vertex;
104
     }
105 d44ac567 Leszek Koltunski
106 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 a51fe521 Leszek Koltunski
  private void buildGrid(float[] attribs)
109 adb2661c Leszek Koltunski
     {
110
     boolean lastBlockIsNE = false;
111
     boolean currentBlockIsNE;
112
     int vertex = 0;
113
114 69ed1eb4 Leszek Koltunski
     float x,y;
115
     final float X = 1.0f/mCols;
116
     final float Y = 1.0f/mRows;
117
118 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid");
119 adb2661c Leszek Koltunski
120 69ed1eb4 Leszek Koltunski
     y = 0.0f;
121
122 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
123
       {
124 69ed1eb4 Leszek Koltunski
       x = 0.0f;
125
126 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
127
         {
128
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
129
130
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
131
           {
132 a51fe521 Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
133
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
134
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
135
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
136
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
137 adb2661c Leszek Koltunski
           }
138 a51fe521 Leszek Koltunski
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
139
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
140 adb2661c Leszek Koltunski
141
         lastBlockIsNE = currentBlockIsNE;
142 69ed1eb4 Leszek Koltunski
         x+=X;
143 adb2661c Leszek Koltunski
         }
144 69ed1eb4 Leszek Koltunski
145
       y+=Y;
146 adb2661c Leszek Koltunski
       }
147
148 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid done");
149 adb2661c Leszek Koltunski
     }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152 79f172ab Leszek Koltunski
/*
153 a51fe521 Leszek Koltunski
  private static String debug(float[] val, int stop)
154 79f172ab Leszek Koltunski
     {
155
     String ret="";
156
157
     for(int i=0; i<val.length; i++)
158
        {
159
        if( i%stop==0 ) ret+="\n";
160
        ret+=(" "+val[i]);
161
        }
162
163
     return ret;
164
     }
165
*/
166 11fb6ce0 Leszek Koltunski
167 79f172ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 a56bc359 Leszek Koltunski
// PUBLIC API
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170 adb2661c Leszek Koltunski
/**
171
 * Creates the underlying grid of vertices, normals and texture coords.
172
 *
173
 * @param cols Number of columns in the grid.
174
 * @param rows Number of rows in the grid.
175
 */
176 a51fe521 Leszek Koltunski
 public MeshFlat(int cols, int rows)
177
    {
178
    super(0.0f);
179
    computeNumberOfVertices(cols,rows);
180 adb2661c Leszek Koltunski
181 cab7c165 Leszek Koltunski
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
182 adb2661c Leszek Koltunski
183 a51fe521 Leszek Koltunski
    buildGrid(attribs);
184 adb2661c Leszek Koltunski
185 a51fe521 Leszek Koltunski
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
186
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
187 adb2661c Leszek Koltunski
188 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
189 466450b5 Leszek Koltunski
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
190 adb2661c Leszek Koltunski
191 a51fe521 Leszek Koltunski
    mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
192
    mVertAttribs.put(attribs).position(0);
193 466450b5 Leszek Koltunski
194
    setData(numVertices, mVertAttribs);
195 a51fe521 Leszek Koltunski
    }
196
 }