Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshTriangles.java @ 4f81e0c8

1 9099e567 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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 b17aa4aa Leszek Koltunski
/**
24
 * Create a Mesh which approximates an equilateral triangle.
25
 */
26 9099e567 Leszek Koltunski
public class MeshTriangles extends MeshBase
27
  {
28
  private int numVertices;
29
  private int remainingVert;
30
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 e54bfada Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
34 9099e567 Leszek Koltunski
     {
35
     remainingVert--;
36
37 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
38
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y-0.5f;
39
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
40 9099e567 Leszek Koltunski
41 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
42
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
43
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
44 9099e567 Leszek Koltunski
45 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB  ] = (x-0.5f);
46
     attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+1] = (y-0.5f);
47
     attribs1[VERT1_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f   ;  // Inflated surface needs to be slightly in front
48 9099e567 Leszek Koltunski
49 e54bfada Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
50
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
51 1e672c1d Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + ASS_ATTRIB  ] = DEFAULT_ASSOCIATION;
52 9099e567 Leszek Koltunski
53
     return vertex+1;
54
     }
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
  private void computeNumberOfVertices(int level)
59
     {
60
     numVertices = level*(level+2);
61
     remainingVert = numVertices;
62
     }
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 e54bfada Leszek Koltunski
  private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs1, float[] attribs2)
67 9099e567 Leszek Koltunski
    {
68
    for(int i=0; i<length; i++)
69
      {
70 e54bfada Leszek Koltunski
      vertex = addVertex(vertex, sx   , sy   , attribs1, attribs2);
71
      vertex = addVertex(vertex, sx+dx, sy+dy, attribs1, attribs2);
72 9099e567 Leszek Koltunski
      sx += 2*dx;
73
      }
74
75 e54bfada Leszek Koltunski
    vertex = addVertex(vertex, sx, sy, attribs1, attribs2);
76 9099e567 Leszek Koltunski
77
    return vertex;
78
    }
79
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82 e54bfada Leszek Koltunski
  private void buildGrid(float[] attribs1, float[] attribs2, int level)
83 9099e567 Leszek Koltunski
     {
84
     float sx = 0.0f;
85
     float sy = 0.0f;
86
     float dx = 0.5f/level;
87 b35570ad Leszek Koltunski
     float dy = 1.0f/level;
88 9099e567 Leszek Koltunski
     int vertex = 0;
89
90
     for(int row=level; row>=1; row--)
91
       {
92 e54bfada Leszek Koltunski
       vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs1,attribs2);
93 9099e567 Leszek Koltunski
94
       sx += 2*dx*(row-0.5f);
95
       sy += dy;
96
       dx *= -1;
97
       }
98
     }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// PUBLIC API
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
  /**
104
   * Creates the underlying grid of vertices with the usual attribs which approximates an equilateral
105
   * triangle.
106
   *
107
   * @param level Specifies the level of slicing. Valid values: level &ge; 1
108
   */
109
  public MeshTriangles(int level)
110
     {
111 0f10a0b6 Leszek Koltunski
     super();
112 9099e567 Leszek Koltunski
113
     computeNumberOfVertices(level);
114
115 e54bfada Leszek Koltunski
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
116
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
117
118
     buildGrid(attribs1, attribs2, level);
119 9099e567 Leszek Koltunski
120
     if( remainingVert!=0 )
121
       android.util.Log.d("MeshTriangles", "remainingVert " +remainingVert );
122
123 e54bfada Leszek Koltunski
     setAttribs(attribs1, attribs2);
124 9099e567 Leszek Koltunski
     }
125 cbd502ec Leszek Koltunski
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
/**
128 4f81e0c8 Leszek Koltunski
 * Copy constructor.
129 cbd502ec Leszek Koltunski
 */
130 4f81e0c8 Leszek Koltunski
  public MeshTriangles(MeshTriangles mesh, boolean deep)
131 cbd502ec Leszek Koltunski
    {
132 4f81e0c8 Leszek Koltunski
    super(mesh,deep);
133 cbd502ec Leszek Koltunski
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/**
137 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
138
 *
139
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
140
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
141
 *             coordinates and effect assocciations, is always deep copied)
142 cbd502ec Leszek Koltunski
 */
143 4f81e0c8 Leszek Koltunski
  public MeshTriangles copy(boolean deep)
144 cbd502ec Leszek Koltunski
    {
145 4f81e0c8 Leszek Koltunski
    return new MeshTriangles(this,deep);
146 cbd502ec Leszek Koltunski
    }
147 9099e567 Leszek Koltunski
  }