Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshFlat.java @ a16bf106

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 6c00149d Leszek Koltunski
package org.distorted.library.mesh;
21 6a06a912 Leszek Koltunski
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23 e0b6c593 Leszek Koltunski
/**
24 05403bba Leszek Koltunski
 * Create a flat, rectangular grid.
25 e0b6c593 Leszek Koltunski
 * <p>
26
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
27 26df012c Leszek Koltunski
 * effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
28 e0b6c593 Leszek Koltunski
 */
29 715e7726 Leszek Koltunski
public class MeshFlat extends MeshBase
30 6a06a912 Leszek Koltunski
  {
31 adb2661c Leszek Koltunski
  private int mCols, mRows;
32
  private int remainingVert;
33 da681e7e Leszek Koltunski
  private int numVertices;
34 a16bf106 Leszek Koltunski
  private float mInfCorr;
35 6a06a912 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37 cacc63de Leszek Koltunski
// Create a flat, full grid.
38 6a06a912 Leszek Koltunski
39 a51fe521 Leszek Koltunski
  private void computeNumberOfVertices(int cols, int rows)
40 adb2661c Leszek Koltunski
     {
41 79f172ab Leszek Koltunski
     mRows=rows;
42
     mCols=cols;
43 d44ac567 Leszek Koltunski
44 a16bf106 Leszek Koltunski
     mInfCorr = mRows>mCols ? mRows:mCols;
45 554ce72b Leszek Koltunski
46 79f172ab Leszek Koltunski
     if( cols==1 && rows==1 )
47
       {
48 da681e7e Leszek Koltunski
       numVertices = 4;
49 79f172ab Leszek Koltunski
       }
50
     else
51 adb2661c Leszek Koltunski
       {
52 da681e7e Leszek Koltunski
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
53 a51fe521 Leszek Koltunski
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
54 79f172ab Leszek Koltunski
       }
55 d44ac567 Leszek Koltunski
56 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
57 d44ac567 Leszek Koltunski
58 da681e7e Leszek Koltunski
     remainingVert = numVertices;
59 adb2661c Leszek Koltunski
     }
60 d44ac567 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 6f2d931d Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
68
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
69 7a5e538a Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
70 6a06a912 Leszek Koltunski
71 6f2d931d Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
72
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
73
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
74 6a06a912 Leszek Koltunski
75 a16bf106 Leszek Koltunski
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = (x-0.5f)*mInfCorr;
76
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y)*mInfCorr;
77
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f   *mInfCorr;  // Inflated surface needs to be slightly in front
78 6f2d931d Leszek Koltunski
79
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
80
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
81 6a06a912 Leszek Koltunski
82 adb2661c Leszek Koltunski
     return vertex+1;
83
     }
84 466450b5 Leszek Koltunski
85 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86 d44ac567 Leszek Koltunski
87 a51fe521 Leszek Koltunski
  private int repeatLast(int vertex, float[] attribs)
88 adb2661c Leszek Koltunski
     {
89 466450b5 Leszek Koltunski
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
90 6a06a912 Leszek Koltunski
91 adb2661c Leszek Koltunski
     if( vertex>0 )
92
       {
93 6f2d931d Leszek Koltunski
       remainingVert--;
94
95
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
96
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
97
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
98
99
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
100
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
101
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
102
103
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB  ];
104
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
105
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
106
107
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
108
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
109 d44ac567 Leszek Koltunski
110 adb2661c Leszek Koltunski
       vertex++;
111
       }
112 6a06a912 Leszek Koltunski
113 adb2661c Leszek Koltunski
     return vertex;
114
     }
115 d44ac567 Leszek Koltunski
116 adb2661c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118 a51fe521 Leszek Koltunski
  private void buildGrid(float[] attribs)
119 adb2661c Leszek Koltunski
     {
120
     boolean lastBlockIsNE = false;
121
     boolean currentBlockIsNE;
122
     int vertex = 0;
123
124 69ed1eb4 Leszek Koltunski
     float x,y;
125
     final float X = 1.0f/mCols;
126
     final float Y = 1.0f/mRows;
127
128 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid");
129 adb2661c Leszek Koltunski
130 69ed1eb4 Leszek Koltunski
     y = 0.0f;
131
132 adb2661c Leszek Koltunski
     for(int row=0; row<mRows; row++)
133
       {
134 69ed1eb4 Leszek Koltunski
       x = 0.0f;
135
136 adb2661c Leszek Koltunski
       for(int col=0; col<mCols; col++)
137
         {
138
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
139
140
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
141
           {
142 a51fe521 Leszek Koltunski
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
143
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
144
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
145
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
146
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
147 adb2661c Leszek Koltunski
           }
148 a51fe521 Leszek Koltunski
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
149
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
150 adb2661c Leszek Koltunski
151
         lastBlockIsNE = currentBlockIsNE;
152 69ed1eb4 Leszek Koltunski
         x+=X;
153 adb2661c Leszek Koltunski
         }
154 69ed1eb4 Leszek Koltunski
155
       y+=Y;
156 adb2661c Leszek Koltunski
       }
157
158 466450b5 Leszek Koltunski
     //android.util.Log.d("MeshFlat", "buildGrid done");
159 adb2661c Leszek Koltunski
     }
160
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162 79f172ab Leszek Koltunski
/*
163 a51fe521 Leszek Koltunski
  private static String debug(float[] val, int stop)
164 79f172ab Leszek Koltunski
     {
165
     String ret="";
166
167
     for(int i=0; i<val.length; i++)
168
        {
169
        if( i%stop==0 ) ret+="\n";
170
        ret+=(" "+val[i]);
171
        }
172
173
     return ret;
174
     }
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 6f2d931d Leszek Koltunski
    float[] attribs= new float[VERT_ATTRIBS*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 6f2d931d Leszek Koltunski
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,VERT_ATTRIBS) );
197 adb2661c Leszek Koltunski
198 a51fe521 Leszek Koltunski
    if( remainingVert!=0 )
199 466450b5 Leszek Koltunski
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
200 adb2661c Leszek Koltunski
201 227b9bca Leszek Koltunski
    setAttribs(attribs);
202 a51fe521 Leszek Koltunski
    }
203
 }