Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffect.java @ 9becf30e

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 org.distorted.library.main.DistortedEffects;
23
import org.distorted.library.type.Static4D;
24

    
25
import java.lang.reflect.Method;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
30
 */
31
public abstract class VertexEffect extends Effect
32
  {
33
  /**
34
   * 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
35
   */
36
  public static final int NUM_FLOAT_UNIFORMS = 12;
37
  /**
38
   * 4: name, AND association, reserved, EQU Association
39
   */
40
  public static final int NUM_INT_UNIFORMS = 4;
41

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

    
48
  private static String mFullGLSL = "";
49
  private static int mFullEnabled = 0;
50
  private static boolean mFullPrepared = false;
51

    
52
  final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
53

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

    
56
  VertexEffect(EffectName name)
57
    {
58
    super(name);
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private static String retSection(int effect, String code)
64
    {
65
    return
66

    
67
        "if( vProperties[i].x =="+effect+" )\n" +
68
          "{\n" +
69
           code +"\n" +
70
          "}\n" +
71
        "else\n";
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
76

    
77
  private static void prepareFull()
78
    {
79
    Method method;
80

    
81
    for(EffectName name: EffectName.values())
82
      {
83
      if( name.getType() == EffectType.VERTEX )
84
        {
85
        Class<? extends Effect> cls = name.getEffectClass();
86

    
87
        try
88
          {
89
          method = cls.getDeclaredMethod("code");
90
          }
91
        catch(NoSuchMethodException ex)
92
          {
93
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
94
          method = null;
95
          }
96

    
97
        try
98
          {
99
          if( method!=null )
100
            {
101
            Object value = method.invoke(null);
102
            String code = (String)value;
103
            mFullGLSL += retSection(name.ordinal(),code);
104
            mFullEnabled++;
105
            }
106
          }
107
        catch(Exception ex)
108
          {
109
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
110
          }
111
        }
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
// prepare code to be injected into the 'main_vertex_shader' main() function.
117

    
118
  static void addEffect(EffectName name, String code)
119
    {
120
    int effect = name.ordinal();
121

    
122
    if( !mEnabled[effect] )
123
      {
124
      mEnabled[effect] = true;
125
      mNumEnabled ++;
126
      mGLSL += retSection(effect,code);
127
      }
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/**
132
 * Only for use by the library itself.
133
 *
134
 * @y.exclude
135
 */
136
  public static String getGLSL()
137
    {
138
    return mGLSL + "{}";
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
/**
143
 * Only for use by the library itself.
144
 *
145
 * @y.exclude
146
 */
147
  public static String getAllGLSL()
148
    {
149
    if( !mFullPrepared )
150
      {
151
      prepareFull();
152
      mFullPrepared = true;
153
      }
154

    
155
    return mFullGLSL + "{}";
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
/**
160
 * Only for use by the library itself.
161
 *
162
 * @y.exclude
163
 */
164
  public static int getAllEnabled()
165
    {
166
    if( !mFullPrepared )
167
      {
168
      prepareFull();
169
      mFullPrepared = true;
170
      }
171

    
172
    return mFullEnabled;
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  static void destroyStatics()
178
    {
179
    mNumEnabled = 0;
180
    mGLSL = "";
181
    mFullEnabled= 0;
182
    mFullGLSL = "";
183
    mFullPrepared = false;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// PUBLIC API
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
/**
190
 * Return the number of Vertex effects enabled.
191
 */
192
  public static int getNumEnabled()
193
    {
194
    return mNumEnabled;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
/**
199
 * Set Mesh association.
200
 *
201
 * This creates an association between a Component of a Mesh and this Vertex Effect.
202
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
203
 * will only be active on vertices of Components such that
204
 *
205
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
206
 *
207
 * (see main_vertex_shader)
208
 *
209
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
210
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
211
 */
212
  public void setMeshAssociation(int andAssociation, int equAssociation)
213
    {
214
    mAndAssociation = andAssociation;
215
    mEquAssociation = equAssociation;
216

    
217
    DistortedEffects.setAssociation(getID());
218
    }
219
  }
(20-20/32)