Project

General

Profile

« Previous | Next » 

Revision 9099e567

Added by Leszek Koltunski over 4 years ago

New Mesh 'Triangles' and updated Inflate & Generic apps to test it.

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.java
79 79
       {
80 80
       mTextureMap = new float[8];
81 81

  
82
       mTextureMap[ 0] = 0.0f;  // LLX
83
       mTextureMap[ 1] = 0.0f;  // LLY
84
       mTextureMap[ 2] = 0.0f;  // ULX
85
       mTextureMap[ 3] = 1.0f;  // ULY
86
       mTextureMap[ 4] = 1.0f;  // URX
87
       mTextureMap[ 5] = 1.0f;  // URY
88
       mTextureMap[ 6] = 1.0f;  // LRX
89
       mTextureMap[ 7] = 0.0f;  // LRY
82
       mTextureMap[ 0] = 0.0f;  // LD_X
83
       mTextureMap[ 1] = 0.0f;  // LD_Y
84
       mTextureMap[ 2] = 0.0f;  // LU_X
85
       mTextureMap[ 3] = 1.0f;  // LU_Y
86
       mTextureMap[ 4] = 1.0f;  // RU_X
87
       mTextureMap[ 5] = 1.0f;  // RU_Y
88
       mTextureMap[ 6] = 1.0f;  // RD_X
89
       mTextureMap[ 7] = 0.0f;  // RD_Y
90 90
       }
91 91
     Component(Component original)
92 92
       {
......
101 101

  
102 102
///////////////////////////////////////////////////////////////////////////////////////////////////
103 103

  
104
   MeshBase(float width, float height, float depth)
104
   MeshBase(float sx, float sy, float sz)
105 105
     {
106
     mStretchX = width;
107
     mStretchY = height;
108
     mStretchZ = depth;
106
     mStretchX = sx;
107
     mStretchY = sy;
108
     mStretchZ = sz;
109 109

  
110 110
     mShowNormals = false;
111 111
     mInflate     = 0.0f;
......
390 390
     {
391 391

  
392 392
     }
393

  
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
/**
396
 * Sets texture maps for all components of this mesh.
397
 * <p>
398
 * Please note that calling this once with the complete list of Maps will be much faster than
399
 * calling it repeatedly with one Maps at a time, as we have to reallocate the array of vertices
400
 * each time.
401
 */
402
   public void setTextureMap(float[][] maps)
403
     {
404
     int components = mComponent.size();
405

  
406
     for(int comp=0; comp<components; comp++)
407
       {
408

  
409
       }
410
     }
393 411
   }
394 412

  
395 413

  
src/main/java/org/distorted/library/mesh/MeshTriangles.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

  
24
public class MeshTriangles extends MeshBase
25
  {
26
  private static final float HEIGHT = (float)Math.sqrt(3)*0.5f;
27
  private int numVertices;
28
  private int remainingVert;
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

  
32
  private int addVertex(int vertex, float x, float y, float[] attribs)
33
     {
34
     remainingVert--;
35

  
36
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
37
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = y-HEIGHT/3;
38
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.0f;
39

  
40
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = 0.0f;
41
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 0.0f;
42
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = 1.0f;
43

  
44
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = (x-0.5f);
45
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = (y-HEIGHT/3);
46
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.01f   ;  // Inflated surface needs to be slightly in front
47

  
48
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
49
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = y;
50

  
51
     return vertex+1;
52
     }
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

  
56
  private void computeNumberOfVertices(int level)
57
     {
58
     numVertices = level*(level+2);
59
     remainingVert = numVertices;
60
     }
61

  
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

  
64
  private int buildRow(int vertex,float sx, float sy, int length, float dx, float dy, float[] attribs)
65
    {
66
    for(int i=0; i<length; i++)
67
      {
68
      vertex = addVertex(vertex, sx   , sy   , attribs);
69
      vertex = addVertex(vertex, sx+dx, sy+dy, attribs);
70
      sx += 2*dx;
71
      }
72

  
73
    vertex = addVertex(vertex, sx, sy, attribs);
74

  
75
    return vertex;
76
    }
77

  
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

  
80
  private void buildGrid(float[] attribs, int level)
81
     {
82
     float sx = 0.0f;
83
     float sy = 0.0f;
84
     float dx = 0.5f/level;
85
     float dy = HEIGHT/level;
86
     int vertex = 0;
87

  
88
     for(int row=level; row>=1; row--)
89
       {
90
       vertex = buildRow(vertex,sx,sy,row,dx,dy,attribs);
91

  
92
       sx += 2*dx*(row-0.5f);
93
       sy += dy;
94
       dx *= -1;
95
       }
96
     }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// PUBLIC API
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
  /**
102
   * Creates the underlying grid of vertices with the usual attribs which approximates an equilateral
103
   * triangle.
104
   *
105
   * @param level Specifies the level of slicing. Valid values: level &ge; 1
106
   */
107
  public MeshTriangles(int level)
108
     {
109
     super(1,HEIGHT,0);
110

  
111
     computeNumberOfVertices(level);
112

  
113
     float[] attribs= new float[VERT_ATTRIBS*numVertices];
114
     buildGrid(attribs,level);
115

  
116
     if( remainingVert!=0 )
117
       android.util.Log.d("MeshTriangles", "remainingVert " +remainingVert );
118

  
119
     setAttribs(attribs);
120
     }
121
  }

Also available in: Unified diff