Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / MatrixEffect.java @ fa8bc998

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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.effect;
21

    
22
import android.opengl.Matrix;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Abstract class that represents an Effect that works by modifying the ModelView matrix.
27
 */
28
public abstract class MatrixEffect extends Effect
29
  {
30
/**
31
 * 7: 4 per-effect interpolated values + 3 dimensional center.
32
 */
33
  public static final int NUM_UNIFORMS = 7;
34

    
35
  private float[] mMatrix,mTmpArray;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
/**
39
 * Only for use by the library itself.
40
 *
41
 * @y.exclude
42
 */
43
  public abstract void apply(float[] matrix, float[] uniforms, int index);
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
/**
47
 * Only for use by the library itself.
48
 * <p>
49
 * The three values buffer[index], buffer[index+1], buffer[index+2] are assumed to contain the
50
 * x,y,z coordinates of a point. We apply this Matrix effect to this point and overwrite those
51
 * three values.
52
 *
53
 * This is on purpose a static evaluation - if this Effect contains any Dynamics, they will be
54
 * evaluated at 0.
55
 *
56
 * @y.exclude
57
 */
58
 public void applyToPoint(float[] buffer, int index)
59
   {
60
   Matrix.setIdentityM(mMatrix,0);
61
   compute(mTmpArray,0,0,0);
62
   apply(mMatrix, mTmpArray, 0);
63

    
64
   float x = buffer[index  ];
65
   float y = buffer[index+1];
66
   float z = buffer[index+2];
67

    
68
   buffer[index  ] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z + mMatrix[12];
69
   buffer[index+1] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z + mMatrix[13];
70
   buffer[index+2] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z + mMatrix[14];
71
   }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
/**
75
 * Only for use by the library itself.
76
 * <p>
77
 * The three values buffer[index], buffer[index+1], buffer[index+2] are assumed to contain the
78
 * x,y,z coordinates of a vector. We apply this Matrix effect to this vector and overwrite those
79
 * three values.
80
 *
81
 * This is on purpose a static evaluation - if this Effect contains any Dynamics, they will be
82
 * evaluated at 0.
83
 *
84
 * @y.exclude
85
 */
86
 public void applyToVector(float[] buffer, int index)
87
   {
88
   Matrix.setIdentityM(mMatrix,0);
89
   compute(mTmpArray,0,0,0);
90
   apply(mMatrix, mTmpArray, 0);
91

    
92
   float x = buffer[index  ];
93
   float y = buffer[index+1];
94
   float z = buffer[index+2];
95

    
96
   buffer[index  ] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z;
97
   buffer[index+1] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z;
98
   buffer[index+2] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z;
99
   }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
/**
103
 * Only for use by the library itself.
104
 * <p>
105
 * The 9 floats starting at buffer[index] are assumed to contain info about a vertex arranged in
106
 * MeshBase way, i.e.  coord point (x,y,z) ; normal vector (x,y,z) ; inflate vector (x,y,z)
107
 *
108
 * @y.exclude
109
 */
110
 public void applyToVertex(float[] buffer, int index)
111
   {
112
   float x,y,z;
113

    
114
   Matrix.setIdentityM(mMatrix,0);
115
   compute(mTmpArray,0,0,0);
116
   apply(mMatrix, mTmpArray, 0);
117

    
118
   x = buffer[index  ];
119
   y = buffer[index+1];
120
   z = buffer[index+2];
121

    
122
   buffer[index  ] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z + mMatrix[12];
123
   buffer[index+1] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z + mMatrix[13];
124
   buffer[index+2] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z + mMatrix[14];
125

    
126
   x = buffer[index+3];
127
   y = buffer[index+4];
128
   z = buffer[index+5];
129

    
130
   buffer[index+3] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z;
131
   buffer[index+4] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z;
132
   buffer[index+5] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z;
133

    
134
   x = buffer[index+6];
135
   y = buffer[index+7];
136
   z = buffer[index+8];
137

    
138
   buffer[index+6] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z;
139
   buffer[index+7] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z;
140
   buffer[index+8] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z;
141
   }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
// empty function for completeness
145

    
146
  static void destroyStatics()
147
    {
148

    
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  MatrixEffect(EffectName name)
154
    {
155
    super(name);
156

    
157
    mMatrix  = new float[16];
158
    mTmpArray= new float[4];
159
    }
160
  }
(11-11/26)