~josejuan-sanchez/+junk/original-jhv-experimental-version

« back to all changes in this revision

Viewing changes to src/viewmodel/src/org/helioviewer/viewmodel/view/opengl/shader/GLTextureCoordinate.java

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-02-05 13:32:08 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20130205133208-dfz1sh1uge5pjkny
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.helioviewer.viewmodel.view.opengl.shader;
 
2
 
 
3
import javax.media.opengl.GL;
 
4
 
 
5
import org.helioviewer.base.logging.Log;
 
6
 
 
7
/**
 
8
 * Class representing a part of a OpenGL texture coordinate.
 
9
 * 
 
10
 * <p>
 
11
 * This class provides the capability to set only a part of an actual texture
 
12
 * coordinate. Therefore, it manages a static array, shared by all
 
13
 * GLTextureCoordinates. Thus, to work correctly, pure OpenGL-calls to set the
 
14
 * texture coordinate are forbidden.
 
15
 * 
 
16
 * 
 
17
 * <p>
 
18
 * Also, this object is supposed to provide the capability to write the values
 
19
 * assigned to it. To prevent overlaps, only use the implementations returned by
 
20
 * other classes, such as {@link GLShaderBuilder} and
 
21
 * {@link org.helioviewer.viewmodel.view.opengl.GLTextureHelper}, which manage
 
22
 * the utilization of the entire set. Overriding this abstract class may lead to
 
23
 * unwanted behavior.
 
24
 * 
 
25
 * @author Markus Langenberg
 
26
 */
 
27
public abstract class GLTextureCoordinate {
 
28
 
 
29
    private int target;
 
30
    private int offset;
 
31
    private int length;
 
32
    private String identifier;
 
33
 
 
34
    private static float currentValues[][];
 
35
 
 
36
    /**
 
37
     * Default constructor.
 
38
     * 
 
39
     * @param target
 
40
     *            OpenGL constant, representing the texture coordinate, such as
 
41
     *            GL_TEXTURE0 or GL_TEXTURE3
 
42
     * @param offset
 
43
     *            The offset within the 4d-coordinate. Has to be within [0, 3]
 
44
     * @param length
 
45
     *            The length of the subset. Has to be within [1, 4]
 
46
     * @param identifier
 
47
     *            The identifier for this coordinate, which has to be used in
 
48
     *            shader programs
 
49
     */
 
50
    protected GLTextureCoordinate(int target, int offset, int length, String identifier) {
 
51
        this.target = target - GL.GL_TEXTURE0;
 
52
        this.offset = offset;
 
53
        this.length = length;
 
54
        this.identifier = identifier;
 
55
 
 
56
        if (this.target >= currentValues.length || target < 0) {
 
57
            throw new IllegalArgumentException("Target refers to an invalid texture coordinate: GL_TEXTURE" + this.target + ".");
 
58
        }
 
59
 
 
60
        if (offset < 0 || offset > 3) {
 
61
            throw new IllegalArgumentException("Offset out of bounds: " + offset + ".");
 
62
        }
 
63
 
 
64
        if (length < 1 || length > 4) {
 
65
            throw new IllegalArgumentException("Length out of bounds: " + length + ".");
 
66
        }
 
67
 
 
68
        if (offset + length > 4) {
 
69
            throw new IllegalArgumentException("Combination of offset and length out of bounds: " + offset + ", " + length + ".");
 
70
        }
 
71
    }
 
72
 
 
73
    /**
 
74
     * Initializes this class.
 
75
     * 
 
76
     * This functions has to be called before using the class.
 
77
     * 
 
78
     * @param gl
 
79
     *            Valid reference to the current gl object
 
80
     */
 
81
    public static void init(GL gl) {
 
82
        Log.debug(">> GLTextureCoordinate.init(GL) > Initialize GLTextureCoordinate");
 
83
        int tmp[] = new int[1];
 
84
        gl.glGetIntegerv(GL.GL_MAX_TEXTURE_COORDS, tmp, 0);
 
85
 
 
86
        currentValues = new float[tmp[0]][4];
 
87
    }
 
88
 
 
89
    /**
 
90
     * Sets the values managed by this object.
 
91
     * 
 
92
     * This function may only be called for 1d-subsets.
 
93
     * 
 
94
     * @param gl
 
95
     *            Valid reference to the current gl object
 
96
     * @param value1
 
97
     *            First component to set
 
98
     */
 
99
    public void setValue(GL gl, float value1) {
 
100
        if (length != 1) {
 
101
            throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
 
102
        }
 
103
 
 
104
        currentValues[target][offset] = value1;
 
105
        gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
 
106
    }
 
107
 
 
108
    /**
 
109
     * Sets the values managed by this object.
 
110
     * 
 
111
     * This function may only be called for 2d-subsets.
 
112
     * 
 
113
     * @param gl
 
114
     *            Valid reference to the current gl object
 
115
     * @param value1
 
116
     *            First component to set
 
117
     * @param value2
 
118
     *            Second component to set
 
119
     */
 
120
    public void setValue(GL gl, float value1, float value2) {
 
121
        if (length != 2) {
 
122
            throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
 
123
        }
 
124
 
 
125
        currentValues[target][offset] = value1;
 
126
        currentValues[target][offset + 1] = value2;
 
127
        gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
 
128
    }
 
129
 
 
130
    /**
 
131
     * Sets the values managed by this object.
 
132
     * 
 
133
     * This function may only be called for 3d-subsets.
 
134
     * 
 
135
     * @param gl
 
136
     *            Valid reference to the current gl object
 
137
     * @param value1
 
138
     *            First component to set
 
139
     * @param value2
 
140
     *            Second component to set
 
141
     * @param value3
 
142
     *            Third component to set
 
143
     */
 
144
    public void setValue(GL gl, float value1, float value2, float value3) {
 
145
        if (length != 3) {
 
146
            throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
 
147
        }
 
148
 
 
149
        currentValues[target][offset] = value1;
 
150
        currentValues[target][offset + 1] = value2;
 
151
        currentValues[target][offset + 2] = value3;
 
152
        gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
 
153
    }
 
154
 
 
155
    /**
 
156
     * Sets the values managed by this object.
 
157
     * 
 
158
     * This function may only be called for 4d-subsets.
 
159
     * 
 
160
     * @param gl
 
161
     *            Valid reference to the current gl object
 
162
     * @param value1
 
163
     *            First component to set
 
164
     * @param value2
 
165
     *            Second component to set
 
166
     * @param value3
 
167
     *            Third component to set
 
168
     * @param value4
 
169
     *            Forth component to set
 
170
     */
 
171
    public void setValue(GL gl, float value1, float value2, float value3, float value4) {
 
172
        if (length != 4) {
 
173
            throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
 
174
        }
 
175
 
 
176
        currentValues[target][offset] = value1;
 
177
        currentValues[target][offset + 1] = value2;
 
178
        currentValues[target][offset + 2] = value3;
 
179
        currentValues[target][offset + 3] = value4;
 
180
        gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
 
181
    }
 
182
 
 
183
    /**
 
184
     * Sets the values managed by this object.
 
185
     * 
 
186
     * The length of the vector has to be equal to the length of the subset
 
187
     * represented by the is object.
 
188
     * 
 
189
     * @param gl
 
190
     *            Valid reference to the current gl object
 
191
     * @param values
 
192
     *            Array containing all components to set
 
193
     */
 
194
    public void setValue(GL gl, float[] values) {
 
195
        if (length != values.length) {
 
196
            throw new IllegalArgumentException("'values' is supposed to have a length of " + length + ".");
 
197
        }
 
198
 
 
199
        for (int i = 0; i < length; i++) {
 
200
            currentValues[target][offset + i] = values[i];
 
201
        }
 
202
        gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
 
203
    }
 
204
 
 
205
    /**
 
206
     * Returns the identifier for this coordinate, which has to be used in
 
207
     * shader programs.
 
208
     * 
 
209
     * @return The identifier for this coordinate, which has to be used in
 
210
     *         shader programs
 
211
     */
 
212
    public String getIdentifier() {
 
213
        return identifier;
 
214
    }
 
215
 
 
216
    /**
 
217
     * Returns the identifier for this coordinate, which has to be used in
 
218
     * shader programs.
 
219
     * 
 
220
     * <p>
 
221
     * In addition to the basic version of this function, it provides the
 
222
     * capability to repeat the dimension. e.g. for a 1d-subset, setting
 
223
     * repeatDimension to 3 will return <i>identifier.ddd</i> instead of
 
224
     * <i>identifier.d</i>. This might be useful for some shader programs.
 
225
     * 
 
226
     * @param repeatDimension
 
227
     *            Number of repetitions of the dimension
 
228
     * @return The identifier for this coordinate, which has to be used in
 
229
     *         shader programs
 
230
     */
 
231
    public String getIdentifier(int repeatDimension) {
 
232
        String output = identifier;
 
233
        String repeat = identifier.substring(identifier.length() - length);
 
234
 
 
235
        for (int i = 1; i < repeatDimension; i++) {
 
236
            output += repeat;
 
237
        }
 
238
 
 
239
        return output;
 
240
    }
 
241
}