Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effect;
22

    
23
import org.distorted.library.effectqueue.EffectQueue;
24
import org.distorted.library.type.Static4D;
25

    
26
import java.lang.reflect.Method;
27
import java.util.ArrayList;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
32
 */
33
public abstract class VertexEffect extends Effect
34
  {
35
  /**
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
   * 4: name, AND association, reserved, EQU Association
41
   */
42
  public static final int NUM_INT_UNIFORMS = 4;
43

    
44
  static final int VALUES_OFFSET = 0;
45
  static final int CENTER_OFFSET = 5;
46
  static final int REGION_OFFSET = 8;
47
  private static String mGLSL = "";
48
  private static int mNumEnabled = 0;
49

    
50
  private static String mFullGLSL = "";
51
  private static int mFullEnabled = 0;
52
  private static boolean mFullPrepared = false;
53

    
54
  final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
55

    
56
  ArrayList<EffectQueue> mQueues;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  VertexEffect(EffectName name)
61
    {
62
    super(name);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  private static String retSection(int effect, String code)
68
    {
69
    return
70

    
71
        "if( vProperties[i].x =="+effect+" )\n" +
72
          "{\n" +
73
           code +"\n" +
74
          "}\n" +
75
        "else\n";
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
135
/**
136
 * Only for use by the library itself.
137
 *
138
 * @y.exclude
139
 */
140
  public static String getGLSL()
141
    {
142
    return mGLSL + "{}";
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  static void destroyStatics()
182
    {
183
    mNumEnabled = 0;
184
    mGLSL = "";
185
    mFullEnabled= 0;
186
    mFullGLSL = "";
187
    mFullPrepared = false;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
// PUBLIC API
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
/**
194
 * Return the number of Vertex effects enabled.
195
 */
196
  public static int getNumEnabled()
197
    {
198
    return mNumEnabled;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
228
/**
229
 * Set Mesh association.
230
 *
231
 * 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
 *
235
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
236
 *
237
 * (see main_vertex_shader)
238
 *
239
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
240
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
241
 */
242
  public void setMeshAssociation(int andAssociation, int equAssociation)
243
    {
244
    mAndAssociation = andAssociation;
245
    mEquAssociation = equAssociation;
246

    
247
    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
    }
256
  }
(22-22/34)