Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffect.java @ 96e3b88a

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.type.Static4D;
23

    
24
import java.lang.reflect.Method;
25

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

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

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

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
66
        "if( vName[i]=="+effect+" )\n" +
67
          "{\n" +
68
           code +"\n" +
69
          "}\n" +
70
        "else\n";
71
    }
72

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

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

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

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

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

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

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

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

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

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

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

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

    
171
    return mFullEnabled;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

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

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