Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshFlat.java @ 554ce72b

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.mesh;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
/**
24
 * Create a flat, rectangular grid.
25
 * <p>
26
 * Perfect if you just want to display a flat Texture. If you are not planning to apply any VERTEX
27
 * effects to it, use MeshFlat(1,1), i.e. a Quad. Otherwise, create more vertices for more realistic effects!
28
 */
29
public class MeshFlat extends MeshBase
30
  {
31
  private int mCols, mRows;
32
  private int remainingVert;
33
  private int numVertices;
34
  private float mInfCorrX, mInfCorrY;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
// Create a flat, full grid.
38

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

    
44
     int max = mRows>mCols ? mRows:mCols;
45
     mInfCorrX = (float)max/mCols;
46
     mInfCorrY = (float)max/mRows;
47

    
48
     if( cols==1 && rows==1 )
49
       {
50
       numVertices = 4;
51
       }
52
     else
53
       {
54
       numVertices = 2*( mRows*mCols +2*mRows - 1) +2*(mCols>=2 ? mRows:0) +
55
                     (mCols>=2 && mRows>=2 ? 2*mRows-2 : 1);
56
       }
57

    
58
     //android.util.Log.e("MeshFlat","vertices="+numVertices+" rows="+mRows+" cols="+mCols);
59

    
60
     remainingVert = numVertices;
61
     }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
69
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
70
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
71
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
72

    
73
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
74
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
75
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
76

    
77
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = (x-0.5f)*mInfCorrX;
78
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (0.5f-y)*mInfCorrY;
79

    
80
     // TODO: this better be non-zero, so that when the GLOW gets rendered it is slightly above
81
     // the original MeshFlat surface, but simply setting this to non-zero here does not work,
82
     // because MeshFlat's u_objD.z is 0 and this gets multipled by it in the main vertex shader.
83
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;
84

    
85
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
86
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
87

    
88
     return vertex+1;
89
     }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  private int repeatLast(int vertex, float[] attribs)
94
     {
95
     //android.util.Log.e("MeshFlat", "repeating last vertex!");
96

    
97
     if( vertex>0 )
98
       {
99
       remainingVert--;
100

    
101
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
102
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
103
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
104

    
105
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
106
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
107
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
108

    
109
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB  ];
110
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
111
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
112

    
113
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
114
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
115

    
116
       vertex++;
117
       }
118

    
119
     return vertex;
120
     }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void buildGrid(float[] attribs)
125
     {
126
     boolean lastBlockIsNE = false;
127
     boolean currentBlockIsNE;
128
     int vertex = 0;
129

    
130
     float x,y;
131
     final float X = 1.0f/mCols;
132
     final float Y = 1.0f/mRows;
133

    
134
     //android.util.Log.d("MeshFlat", "buildGrid");
135

    
136
     y = 0.0f;
137

    
138
     for(int row=0; row<mRows; row++)
139
       {
140
       x = 0.0f;
141

    
142
       for(int col=0; col<mCols; col++)
143
         {
144
         currentBlockIsNE = (2*row<=mRows-1)^(2*col<=mCols-1);
145

    
146
         if( col==0 || (lastBlockIsNE^currentBlockIsNE) )
147
           {
148
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
149
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?0:Y), attribs);
150
           if( row!=0 && col==0 ) vertex = repeatLast(vertex,attribs);
151
           if( lastBlockIsNE^currentBlockIsNE)  vertex = repeatLast(vertex,attribs);
152
           vertex= addVertex( vertex, x, y+(currentBlockIsNE?Y:0), attribs);
153
           }
154
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?0:Y), attribs);
155
         vertex= addVertex( vertex, x+X, y+(currentBlockIsNE?Y:0), attribs);
156

    
157
         lastBlockIsNE = currentBlockIsNE;
158
         x+=X;
159
         }
160

    
161
       y+=Y;
162
       }
163

    
164
     //android.util.Log.d("MeshFlat", "buildGrid done");
165
     }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
/*
169
  private static String debug(float[] val, int stop)
170
     {
171
     String ret="";
172

    
173
     for(int i=0; i<val.length; i++)
174
        {
175
        if( i%stop==0 ) ret+="\n";
176
        ret+=(" "+val[i]);
177
        }
178

    
179
     return ret;
180
     }
181
*/
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
// PUBLIC API
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
/**
187
 * Creates the underlying grid of vertices, normals and texture coords.
188
 *
189
 * @param cols Number of columns in the grid.
190
 * @param rows Number of rows in the grid.
191
 */
192
 public MeshFlat(int cols, int rows)
193
    {
194
    super(0.0f);
195
    computeNumberOfVertices(cols,rows);
196

    
197
    float[] attribs= new float[VERT_ATTRIBS*numVertices];
198

    
199
    buildGrid(attribs);
200

    
201
    //android.util.Log.e("MeshFlat", "dataLen="+numVertices);
202
    //android.util.Log.d("MeshFlat", "attribs: "+debug(attribs,VERT_ATTRIBS) );
203

    
204
    if( remainingVert!=0 )
205
      android.util.Log.d("MeshFlat", "remainingVert " +remainingVert );
206

    
207
    setAttribs(attribs);
208
    }
209
 }
(3-3/3)