Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffect.java @ 7958d843

1 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 8eccf334 Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 8eccf334 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 8eccf334 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.effect;
21
22 43814a57 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
23 dd89c7f4 Leszek Koltunski
import org.distorted.library.type.Static4D;
24
25 f046b159 Leszek Koltunski
import java.lang.reflect.Method;
26 43814a57 Leszek Koltunski
import java.util.ArrayList;
27 f046b159 Leszek Koltunski
28 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
29 faa3ff56 Leszek Koltunski
/**
30 a20f274f Leszek Koltunski
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
31 faa3ff56 Leszek Koltunski
 */
32 8eccf334 Leszek Koltunski
public abstract class VertexEffect extends Effect
33
  {
34 96e3b88a Leszek Koltunski
  /**
35
   * 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
36
   */
37
  public static final int NUM_FLOAT_UNIFORMS = 12;
38
  /**
39 24804c15 Leszek Koltunski
   * 4: name, AND association, reserved, EQU Association
40 96e3b88a Leszek Koltunski
   */
41 24804c15 Leszek Koltunski
  public static final int NUM_INT_UNIFORMS = 4;
42 96e3b88a Leszek Koltunski
43 b24e4719 Leszek Koltunski
  static final int VALUES_OFFSET = 0;
44
  static final int CENTER_OFFSET = 5;
45
  static final int REGION_OFFSET = 8;
46 7cd24173 leszek
  private static String mGLSL = "";
47
  private static int mNumEnabled = 0;
48 15aa7d94 Leszek Koltunski
49 f046b159 Leszek Koltunski
  private static String mFullGLSL = "";
50
  private static int mFullEnabled = 0;
51
  private static boolean mFullPrepared = false;
52
53 dd89c7f4 Leszek Koltunski
  final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
54
55 43814a57 Leszek Koltunski
  ArrayList<EffectQueue> mQueues;
56
57 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 a0d5e302 Leszek Koltunski
  VertexEffect(EffectName name)
60 b547aaba leszek
    {
61 9d0d8530 leszek
    super(name);
62 b547aaba leszek
    }
63 7cd24173 leszek
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 f046b159 Leszek Koltunski
  private static String retSection(int effect, String code)
67 7cd24173 leszek
    {
68 f046b159 Leszek Koltunski
    return
69 7cd24173 leszek
70 24804c15 Leszek Koltunski
        "if( vProperties[i].x =="+effect+" )\n" +
71 7cd24173 leszek
          "{\n" +
72
           code +"\n" +
73
          "}\n" +
74
        "else\n";
75
    }
76
77 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
78
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
79
80
  private static void prepareFull()
81
    {
82
    Method method;
83
84
    for(EffectName name: EffectName.values())
85
      {
86
      if( name.getType() == EffectType.VERTEX )
87
        {
88
        Class<? extends Effect> cls = name.getEffectClass();
89
90
        try
91
          {
92
          method = cls.getDeclaredMethod("code");
93
          }
94
        catch(NoSuchMethodException ex)
95
          {
96
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
97
          method = null;
98
          }
99
100
        try
101
          {
102
          if( method!=null )
103
            {
104
            Object value = method.invoke(null);
105
            String code = (String)value;
106
            mFullGLSL += retSection(name.ordinal(),code);
107
            mFullEnabled++;
108
            }
109
          }
110
        catch(Exception ex)
111
          {
112
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
113
          }
114
        }
115
      }
116
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
// prepare code to be injected into the 'main_vertex_shader' main() function.
120
121
  static void addEffect(EffectName name, String code)
122
    {
123
    int effect = name.ordinal();
124
125
    if( !mEnabled[effect] )
126
      {
127
      mEnabled[effect] = true;
128
      mNumEnabled ++;
129
      mGLSL += retSection(effect,code);
130
      }
131
    }
132
133 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
134 faa3ff56 Leszek Koltunski
/**
135
 * Only for use by the library itself.
136
 *
137
 * @y.exclude
138
 */
139 7cd24173 leszek
  public static String getGLSL()
140
    {
141
    return mGLSL + "{}";
142
    }
143
144 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
/**
146
 * Only for use by the library itself.
147
 *
148
 * @y.exclude
149
 */
150
  public static String getAllGLSL()
151
    {
152
    if( !mFullPrepared )
153
      {
154
      prepareFull();
155
      mFullPrepared = true;
156
      }
157
158
    return mFullGLSL + "{}";
159
    }
160
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/**
163
 * Only for use by the library itself.
164
 *
165
 * @y.exclude
166
 */
167
  public static int getAllEnabled()
168
    {
169
    if( !mFullPrepared )
170
      {
171
      prepareFull();
172
      mFullPrepared = true;
173
      }
174
175
    return mFullEnabled;
176
    }
177
178 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
179 2f1f7570 Leszek Koltunski
180
  static void destroyStatics()
181 7cd24173 leszek
    {
182 faa3ff56 Leszek Koltunski
    mNumEnabled = 0;
183
    mGLSL = "";
184 f046b159 Leszek Koltunski
    mFullEnabled= 0;
185
    mFullGLSL = "";
186
    mFullPrepared = false;
187 7cd24173 leszek
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190 faa3ff56 Leszek Koltunski
// PUBLIC API
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
/**
193 6b816678 Leszek Koltunski
 * Return the number of Vertex effects enabled.
194 faa3ff56 Leszek Koltunski
 */
195
  public static int getNumEnabled()
196 7cd24173 leszek
    {
197 faa3ff56 Leszek Koltunski
    return mNumEnabled;
198 7cd24173 leszek
    }
199 bc208a9c Leszek Koltunski
200 43814a57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
201
/**
202
 * Only for use by the library itself.
203
 *
204
 * @y.exclude
205
 */
206
  public void addQueue(EffectQueue queue)
207
    {
208
    if( mQueues==null ) mQueues = new ArrayList<>();
209
210
    mQueues.add(queue);
211
    }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
/**
215
 * Only for use by the library itself.
216
 *
217
 * @y.exclude
218
 */
219
  public void remQueue(EffectQueue queue)
220
    {
221
    if( mQueues==null ) mQueues = new ArrayList<>();
222
223
    mQueues.remove(queue);
224
    }
225
226 bc208a9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
/**
228 1e672c1d Leszek Koltunski
 * Set Mesh association.
229 bc208a9c Leszek Koltunski
 *
230 2aeb75aa Leszek Koltunski
 * This creates an association between a Component of a Mesh and this Vertex Effect.
231
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
232
 * will only be active on vertices of Components such that
233 36d65d88 Leszek Koltunski
 *
234 2aeb75aa Leszek Koltunski
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
235 36d65d88 Leszek Koltunski
 *
236
 * (see main_vertex_shader)
237 bc208a9c Leszek Koltunski
 *
238
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
239 1e672c1d Leszek Koltunski
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
240 bc208a9c Leszek Koltunski
 */
241 2aeb75aa Leszek Koltunski
  public void setMeshAssociation(int andAssociation, int equAssociation)
242 bc208a9c Leszek Koltunski
    {
243 2aeb75aa Leszek Koltunski
    mAndAssociation = andAssociation;
244
    mEquAssociation = equAssociation;
245 9becf30e Leszek Koltunski
246 43814a57 Leszek Koltunski
    long id = getID();
247
    int numQueues = mQueues==null ? 0: mQueues.size();
248
249
    for(int i=0; i<numQueues; i++)
250
      {
251
      EffectQueue queue = mQueues.get(i);
252
      queue.setAssociation(id);
253
      }
254 bc208a9c Leszek Koltunski
    }
255 8eccf334 Leszek Koltunski
  }