Project

General

Profile

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

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

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.main;
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
       numVertices = 4;
48
       }
49
     else
50
       {
51
       numVertices = 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="+numVertices+" rows="+mRows+" cols="+mCols);
56

    
57
     remainingVert = numVertices;
58
     }
59

    
60

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

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

    
67
     attribs[8*vertex  ] = x-0.5f;
68
     attribs[8*vertex+1] = 0.5f-y;
69
     attribs[8*vertex+2] = 0;
70

    
71
     attribs[8*vertex+3] = 0.0f;
72
     attribs[8*vertex+4] = 0.0f;
73
     attribs[8*vertex+5] = 1.0f;
74

    
75
     attribs[8*vertex+6] = x;
76
     attribs[8*vertex+7] = 1.0f-y;
77

    
78
     return vertex+1;
79
     }
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private int repeatLast(int vertex, float[] attribs)
83
     {
84
     remainingVert--;
85

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

    
88
     if( vertex>0 )
89
       {
90
       attribs[8*vertex  ] = attribs[8*vertex-8];
91
       attribs[8*vertex+1] = attribs[8*vertex-7];
92
       attribs[8*vertex+2] = attribs[8*vertex-6];
93
       attribs[8*vertex+3] = attribs[8*vertex-5];
94
       attribs[8*vertex+4] = attribs[8*vertex-4];
95
       attribs[8*vertex+5] = attribs[8*vertex-3];
96
       attribs[8*vertex+6] = attribs[8*vertex-2];
97
       attribs[8*vertex+7] = attribs[8*vertex-1];
98

    
99
       vertex++;
100
       }
101

    
102
     return vertex;
103
     }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  private void buildGrid(float[] attribs)
108
     {
109
     boolean lastBlockIsNE = false;
110
     boolean currentBlockIsNE;
111
     int vertex = 0;
112

    
113
     float x,y;
114
     final float X = 1.0f/mCols;
115
     final float Y = 1.0f/mRows;
116

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

    
119
     y = 0.0f;
120

    
121
     for(int row=0; row<mRows; row++)
122
       {
123
       x = 0.0f;
124

    
125
       for(int col=0; col<mCols; col++)
126
         {
127
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
128

    
129
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
130
           {
131
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
132
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
133
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
134
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
135
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
136
           }
137
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
138
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
139

    
140
         lastBlockIsNE = currentBlockIsNE;
141
         x+=X;
142
         }
143

    
144
       y+=Y;
145
       }
146

    
147
     //android.util.Log.d("BITMAP", "buildGrid done");
148
     }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/*
152
  private static String debug(float[] val, int stop)
153
     {
154
     String ret="";
155

    
156
     for(int i=0; i<val.length; i++)
157
        {
158
        if( i%stop==0 ) ret+="\n";
159
        ret+=(" "+val[i]);
160
        }
161

    
162
     return ret;
163
     }
164
*/
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// PUBLIC API
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
/**
170
 * Creates the underlying grid of vertices, normals and texture coords.
171
 *
172
 * @param cols Number of columns in the grid.
173
 * @param rows Number of rows in the grid.
174
 */
175
 public MeshFlat(int cols, int rows)
176
    {
177
    super(0.0f);
178
    computeNumberOfVertices(cols,rows);
179

    
180
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
181

    
182
    buildGrid(attribs);
183

    
184
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
185
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,8) );
186

    
187
    if( remainingVert!=0 )
188
      android.util.Log.d("BITMAP", "remainingVert " +remainingVert );
189

    
190
    mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
191
    mVertAttribs.put(attribs).position(0);
192
    }
193
 }
(20-20/21)