Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshFlat.java @ 05403bba

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
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// Create a flat, full grid.
39

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

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

    
55
     //android.util.Log.e("BITMAP","vertices="+dataLength+" rows="+mRows+" cols="+mCols);
56

    
57
     remainingVert = dataLength;
58
     }
59

    
60

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

    
63
   private int addVertex(int vertex, int col, int row, float[] position, float[] normal, float[] texture)
64
     {
65
     remainingVert--;
66

    
67
     float x= (float)col/mCols;
68
     float y= (float)row/mRows;
69

    
70
     position[3*vertex  ] = x-0.5f;
71
     position[3*vertex+1] = 0.5f-y;
72
     position[3*vertex+2] = 0;
73

    
74
     texture[2*vertex  ]  = x;
75
     texture[2*vertex+1]  = 1.0f-y;
76

    
77
     normal[3*vertex  ]   = 0.0f;
78
     normal[3*vertex+1]   = 0.0f;
79
     normal[3*vertex+2]   = 1.0f;
80

    
81
     return vertex+1;
82
     }
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
86
     {
87
     remainingVert--;
88

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

    
91
     if( vertex>0 )
92
       {
93
       position[3*vertex  ] = position[3*vertex-3];
94
       position[3*vertex+1] = position[3*vertex-2];
95
       position[3*vertex+2] = position[3*vertex-1];
96

    
97
       normal[3*vertex  ]   = normal[3*vertex-3];
98
       normal[3*vertex+1]   = normal[3*vertex-2];
99
       normal[3*vertex+2]   = normal[3*vertex-1];
100

    
101
       texture[2*vertex  ]  = texture[2*vertex-2];
102
       texture[2*vertex+1]  = texture[2*vertex-1];
103

    
104
       vertex++;
105
       }
106

    
107
     return vertex;
108
     }
109

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

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

    
118
     //android.util.Log.d("BITMAP", "buildGrid");
119

    
120
     for(int row=0; row<mRows; row++)
121
       {
122
       for(int col=0; col<mCols; col++)
123
         {
124
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
125

    
126
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
127
           {
128
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
129
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?0:1), position, normal, texture);
130
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,position,normal,texture);
131
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,position,normal,texture);
132
           vertex= addVertex( vertex, col, row+(currentBlockIsNE?1:0), position, normal, texture);
133
           }
134
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?0:1), position, normal, texture);
135
         vertex= addVertex( vertex, col+1, row+(currentBlockIsNE?1:0), position, normal, texture);
136

    
137
         lastBlockIsNE = currentBlockIsNE;
138
         }
139
       }
140

    
141
     //android.util.Log.d("BITMAP", "buildGrid done");
142
     }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
/*
146
   private static String debug(float[] val, int stop)
147
     {
148
     String ret="";
149

    
150
     for(int i=0; i<val.length; i++)
151
        {
152
        if( i%stop==0 ) ret+="\n";
153
        ret+=(" "+val[i]);
154
        }
155

    
156
     return ret;
157
     }
158
*/
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// PUBLIC API
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/**
163
 * Creates the underlying grid of vertices, normals and texture coords.
164
 *
165
 * @param cols Number of columns in the grid.
166
 * @param rows Number of rows in the grid.
167
 */
168
   public MeshFlat(int cols, int rows)
169
      {
170
      super(0.0f);
171
      computeNumberOfVertices(cols,rows);
172

    
173
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
174
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
175
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
176

    
177
      buildGrid(positionData,normalData,textureData);
178

    
179
      //android.util.Log.e("CUBES","dataLen="+dataLength);
180
      //android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
181
      //android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
182
      //android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
183

    
184
      if( remainingVert!=0 )
185
        android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
186

    
187
      mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
188
      mMeshPositions.put(positionData).position(0);
189

    
190
      mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
191
      mMeshNormals.put(normalData).position(0);
192

    
193
      mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
194
      mMeshTexture.put(textureData).position(0);
195
      }
196
  }
(15-15/16)