Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshTriangle.java @ d23592bb

1 9099e567 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 c4d06f90 Leszek Koltunski
// Copyright 2020 Leszek Koltunski  leszek@koltunski.pl                                          //
3 9099e567 Leszek Koltunski
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6 c4d06f90 Leszek Koltunski
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10 9099e567 Leszek Koltunski
//                                                                                               //
11 c4d06f90 Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 9099e567 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 c4d06f90 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 9099e567 Leszek Koltunski
//                                                                                               //
16 c4d06f90 Leszek Koltunski
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 9099e567 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.mesh;
22
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24 b17aa4aa Leszek Koltunski
/**
25 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)
26 b17aa4aa Leszek Koltunski
 */
27 b948df7a Leszek Koltunski
public class MeshTriangle extends MeshBase
28 9099e567 Leszek Koltunski
  {
29
  private int numVertices;
30
  private int remainingVert;
31
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34 e54bfada Leszek Koltunski
  private int addVertex(int vertex, float x, float y, float[] attribs1, float[] attribs2)
35 9099e567 Leszek Koltunski
     {
36
     remainingVert--;
37
38 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
39
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y-0.5f;
40
     attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
41 9099e567 Leszek Koltunski
42 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
43
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
44
     attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
45 9099e567 Leszek Koltunski
46 e54bfada Leszek Koltunski
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
47
     attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
48 9099e567 Leszek Koltunski
49
     return vertex+1;
50
     }
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54
  private void computeNumberOfVertices(int level)
55
     {
56
     numVertices = level*(level+2);
57
     remainingVert = numVertices;
58
     }
59
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 e54bfada Leszek Koltunski
  private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs1, float[] attribs2)
63 9099e567 Leszek Koltunski
    {
64
    for(int i=0; i<length; i++)
65
      {
66 e54bfada Leszek Koltunski
      vertex = addVertex(vertex, sx   , sy   , attribs1, attribs2);
67
      vertex = addVertex(vertex, sx+dx, sy+dy, attribs1, attribs2);
68 9099e567 Leszek Koltunski
      sx += 2*dx;
69
      }
70
71 e54bfada Leszek Koltunski
    vertex = addVertex(vertex, sx, sy, attribs1, attribs2);
72 9099e567 Leszek Koltunski
73
    return vertex;
74
    }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78 e54bfada Leszek Koltunski
  private void buildGrid(float[] attribs1, float[] attribs2, int level)
79 9099e567 Leszek Koltunski
     {
80
     float sx = 0.0f;
81
     float sy = 0.0f;
82
     float dx = 0.5f/level;
83 b35570ad Leszek Koltunski
     float dy = 1.0f/level;
84 9099e567 Leszek Koltunski
     int vertex = 0;
85
86
     for(int row=level; row>=1; row--)
87
       {
88 e54bfada Leszek Koltunski
       vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs1,attribs2);
89 9099e567 Leszek Koltunski
90
       sx += 2*dx*(row-0.5f);
91
       sy += dy;
92
       dx *= -1;
93
       }
94
     }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// PUBLIC API
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
  /**
100 45b08c37 Leszek Koltunski
   * Creates the underlying grid of vertices with the usual attribs which approximates a triangle
101
   * with vertices at (-0.5,-0.5),(+0.5,-0.5),(0.0,0.5)
102 9099e567 Leszek Koltunski
   *
103
   * @param level Specifies the level of slicing. Valid values: level &ge; 1
104
   */
105 b948df7a Leszek Koltunski
  public MeshTriangle(int level)
106 9099e567 Leszek Koltunski
     {
107 0f10a0b6 Leszek Koltunski
     super();
108 9099e567 Leszek Koltunski
109
     computeNumberOfVertices(level);
110
111 e54bfada Leszek Koltunski
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
112
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
113
114
     buildGrid(attribs1, attribs2, level);
115 9099e567 Leszek Koltunski
116
     if( remainingVert!=0 )
117
       android.util.Log.d("MeshTriangles", "remainingVert " +remainingVert );
118
119 e54bfada Leszek Koltunski
     setAttribs(attribs1, attribs2);
120 9099e567 Leszek Koltunski
     }
121 cbd502ec Leszek Koltunski
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
/**
124 4f81e0c8 Leszek Koltunski
 * Copy constructor.
125 cbd502ec Leszek Koltunski
 */
126 b948df7a Leszek Koltunski
  public MeshTriangle(MeshTriangle mesh, boolean deep)
127 cbd502ec Leszek Koltunski
    {
128 4f81e0c8 Leszek Koltunski
    super(mesh,deep);
129 cbd502ec Leszek Koltunski
    }
130
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
134
 *
135
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
136
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
137 aee078f8 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied)
138 cbd502ec Leszek Koltunski
 */
139 b948df7a Leszek Koltunski
  public MeshTriangle copy(boolean deep)
140 cbd502ec Leszek Koltunski
    {
141 b948df7a Leszek Koltunski
    return new MeshTriangle(this,deep);
142 cbd502ec Leszek Koltunski
    }
143 9099e567 Leszek Koltunski
  }