Project

General

Profile

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

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

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 9becf30e Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
23 dd89c7f4 Leszek Koltunski
import org.distorted.library.type.Static4D;
24
25 f046b159 Leszek Koltunski
import java.lang.reflect.Method;
26
27 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
28 faa3ff56 Leszek Koltunski
/**
29 a20f274f Leszek Koltunski
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
30 faa3ff56 Leszek Koltunski
 */
31 8eccf334 Leszek Koltunski
public abstract class VertexEffect extends Effect
32
  {
33 96e3b88a Leszek Koltunski
  /**
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 24804c15 Leszek Koltunski
   * 4: name, AND association, reserved, EQU Association
39 96e3b88a Leszek Koltunski
   */
40 24804c15 Leszek Koltunski
  public static final int NUM_INT_UNIFORMS = 4;
41 96e3b88a Leszek Koltunski
42 b24e4719 Leszek Koltunski
  static final int VALUES_OFFSET = 0;
43
  static final int CENTER_OFFSET = 5;
44
  static final int REGION_OFFSET = 8;
45 7cd24173 leszek
  private static String mGLSL = "";
46
  private static int mNumEnabled = 0;
47 15aa7d94 Leszek Koltunski
48 f046b159 Leszek Koltunski
  private static String mFullGLSL = "";
49
  private static int mFullEnabled = 0;
50
  private static boolean mFullPrepared = false;
51
52 dd89c7f4 Leszek Koltunski
  final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
53
54 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 a0d5e302 Leszek Koltunski
  VertexEffect(EffectName name)
57 b547aaba leszek
    {
58 9d0d8530 leszek
    super(name);
59 b547aaba leszek
    }
60 7cd24173 leszek
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 f046b159 Leszek Koltunski
  private static String retSection(int effect, String code)
64 7cd24173 leszek
    {
65 f046b159 Leszek Koltunski
    return
66 7cd24173 leszek
67 24804c15 Leszek Koltunski
        "if( vProperties[i].x =="+effect+" )\n" +
68 7cd24173 leszek
          "{\n" +
69
           code +"\n" +
70
          "}\n" +
71
        "else\n";
72
    }
73
74 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
131 faa3ff56 Leszek Koltunski
/**
132
 * Only for use by the library itself.
133
 *
134
 * @y.exclude
135
 */
136 7cd24173 leszek
  public static String getGLSL()
137
    {
138
    return mGLSL + "{}";
139
    }
140
141 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
176 2f1f7570 Leszek Koltunski
177
  static void destroyStatics()
178 7cd24173 leszek
    {
179 faa3ff56 Leszek Koltunski
    mNumEnabled = 0;
180
    mGLSL = "";
181 f046b159 Leszek Koltunski
    mFullEnabled= 0;
182
    mFullGLSL = "";
183
    mFullPrepared = false;
184 7cd24173 leszek
    }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187 faa3ff56 Leszek Koltunski
// PUBLIC API
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
/**
190 6b816678 Leszek Koltunski
 * Return the number of Vertex effects enabled.
191 faa3ff56 Leszek Koltunski
 */
192
  public static int getNumEnabled()
193 7cd24173 leszek
    {
194 faa3ff56 Leszek Koltunski
    return mNumEnabled;
195 7cd24173 leszek
    }
196 bc208a9c Leszek Koltunski
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
/**
199 1e672c1d Leszek Koltunski
 * Set Mesh association.
200 bc208a9c Leszek Koltunski
 *
201 2aeb75aa Leszek Koltunski
 * 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 36d65d88 Leszek Koltunski
 *
205 2aeb75aa Leszek Koltunski
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
206 36d65d88 Leszek Koltunski
 *
207
 * (see main_vertex_shader)
208 bc208a9c Leszek Koltunski
 *
209
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
210 1e672c1d Leszek Koltunski
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
211 bc208a9c Leszek Koltunski
 */
212 2aeb75aa Leszek Koltunski
  public void setMeshAssociation(int andAssociation, int equAssociation)
213 bc208a9c Leszek Koltunski
    {
214 2aeb75aa Leszek Koltunski
    mAndAssociation = andAssociation;
215
    mEquAssociation = equAssociation;
216 9becf30e Leszek Koltunski
217
    DistortedEffects.setAssociation(getID());
218 bc208a9c Leszek Koltunski
    }
219 8eccf334 Leszek Koltunski
  }