Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshTriangle.java @ 97b6c85e

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