Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffect.java @ cc62b34a

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