Project

General

Profile

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

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

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