1
package org.helioviewer.viewmodel.view.opengl.shader;
3
import javax.media.opengl.GL;
5
import org.helioviewer.base.logging.Log;
8
* Class representing a part of a OpenGL texture coordinate.
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.
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
25
* @author Markus Langenberg
27
public abstract class GLTextureCoordinate {
32
private String identifier;
34
private static float currentValues[][];
37
* Default constructor.
40
* OpenGL constant, representing the texture coordinate, such as
41
* GL_TEXTURE0 or GL_TEXTURE3
43
* The offset within the 4d-coordinate. Has to be within [0, 3]
45
* The length of the subset. Has to be within [1, 4]
47
* The identifier for this coordinate, which has to be used in
50
protected GLTextureCoordinate(int target, int offset, int length, String identifier) {
51
this.target = target - GL.GL_TEXTURE0;
54
this.identifier = identifier;
56
if (this.target >= currentValues.length || target < 0) {
57
throw new IllegalArgumentException("Target refers to an invalid texture coordinate: GL_TEXTURE" + this.target + ".");
60
if (offset < 0 || offset > 3) {
61
throw new IllegalArgumentException("Offset out of bounds: " + offset + ".");
64
if (length < 1 || length > 4) {
65
throw new IllegalArgumentException("Length out of bounds: " + length + ".");
68
if (offset + length > 4) {
69
throw new IllegalArgumentException("Combination of offset and length out of bounds: " + offset + ", " + length + ".");
74
* Initializes this class.
76
* This functions has to be called before using the class.
79
* Valid reference to the current gl object
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);
86
currentValues = new float[tmp[0]][4];
90
* Sets the values managed by this object.
92
* This function may only be called for 1d-subsets.
95
* Valid reference to the current gl object
97
* First component to set
99
public void setValue(GL gl, float value1) {
101
throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
104
currentValues[target][offset] = value1;
105
gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
109
* Sets the values managed by this object.
111
* This function may only be called for 2d-subsets.
114
* Valid reference to the current gl object
116
* First component to set
118
* Second component to set
120
public void setValue(GL gl, float value1, float value2) {
122
throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
125
currentValues[target][offset] = value1;
126
currentValues[target][offset + 1] = value2;
127
gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
131
* Sets the values managed by this object.
133
* This function may only be called for 3d-subsets.
136
* Valid reference to the current gl object
138
* First component to set
140
* Second component to set
142
* Third component to set
144
public void setValue(GL gl, float value1, float value2, float value3) {
146
throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
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);
156
* Sets the values managed by this object.
158
* This function may only be called for 4d-subsets.
161
* Valid reference to the current gl object
163
* First component to set
165
* Second component to set
167
* Third component to set
169
* Forth component to set
171
public void setValue(GL gl, float value1, float value2, float value3, float value4) {
173
throw new IllegalArgumentException("Calling this function is not valid for GLTextureCoordinate with lenght " + length + ".");
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);
184
* Sets the values managed by this object.
186
* The length of the vector has to be equal to the length of the subset
187
* represented by the is object.
190
* Valid reference to the current gl object
192
* Array containing all components to set
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 + ".");
199
for (int i = 0; i < length; i++) {
200
currentValues[target][offset + i] = values[i];
202
gl.glMultiTexCoord4fv(target + GL.GL_TEXTURE0, currentValues[target], 0);
206
* Returns the identifier for this coordinate, which has to be used in
209
* @return The identifier for this coordinate, which has to be used in
212
public String getIdentifier() {
217
* Returns the identifier for this coordinate, which has to be used in
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.
226
* @param repeatDimension
227
* Number of repetitions of the dimension
228
* @return The identifier for this coordinate, which has to be used in
231
public String getIdentifier(int repeatDimension) {
232
String output = identifier;
233
String repeat = identifier.substring(identifier.length() - length);
235
for (int i = 1; i < repeatDimension; i++) {