JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
ImageSequence.java
Go to the documentation of this file.
1/**
2 * Copyright 2014-2024 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.util.texture;
29
30import java.io.IOException;
31import java.net.URLConnection;
32import java.util.ArrayList;
33import java.util.List;
34
35import com.jogamp.opengl.GL;
36import com.jogamp.opengl.GLException;
37import com.jogamp.opengl.GLProfile;
38
39import com.jogamp.common.util.IOUtil;
40import com.jogamp.math.Vec4f;
41
42/**
43 * Simple {@link TextureSequence} implementation
44 * allowing {@link #addFrame(GL, Texture) existing textures}
45 * or {@link #addFrame(GL, Class, String, String) image streams}
46 * to be used and <i>replayed</i> as {@link TextureSequence.TextureFrame frames}.
47 */
48public class ImageSequence implements TextureSequence {
49 private final int textureUnit;
50 private final boolean useBuildInTexLookup;
51 private final List<TextureSequence.TextureFrame> frames = new ArrayList<TextureSequence.TextureFrame>();
52 private final int[] texMinMagFilter = { GL.GL_NEAREST, GL.GL_NEAREST };
53 private final int[] texWrapST = { GL.GL_CLAMP_TO_EDGE, GL.GL_CLAMP_TO_EDGE };
54 private volatile int frameIdx = 0;
55 private volatile boolean manualStepping = false;
56 private int textureFragmentShaderHashCode = 0;
57 private boolean aRatioAdjustment = true;
58 private boolean aRatioLbox = false;
59 private final Vec4f aRatioLboxBackColor = new Vec4f();
60
61 public ImageSequence(final int textureUnit, final boolean useBuildInTexLookup) {
62 this.textureUnit = textureUnit;
63 this.useBuildInTexLookup = useBuildInTexLookup;
64 }
65
66 public void setParams(final int magFilter, final int minFilter, final int wrapS, final int wrapT) {
67 texMinMagFilter[0] = minFilter;
68 texMinMagFilter[1] = magFilter;
69 texWrapST[0] = wrapS;
70 texWrapST[1] = wrapT;
71 }
72
73 public final TextureSequence.TextureFrame addFrame(final GL gl, final Texture tex) {
74 return addFrame(gl, new TextureSequence.TextureFrame(tex));
75 }
76 public final TextureSequence.TextureFrame addFrame(final GL gl, final TextureSequence.TextureFrame frame) {
77 frames.add(frame);
78 frame.texture.bind(gl);
83 return frame;
84 }
85 public boolean removeFrame(final TextureFrame tex) {
86 return frames.remove(tex);
87 }
88 public void removeAllFrames() {
89 frames.clear();
90 }
91
92 public final void addFrame(final GL gl, final Class<?> context, final String imageResourcePath, final String imageSuffix) throws IOException {
93 final URLConnection urlConn = IOUtil.getResource(imageResourcePath, context.getClassLoader(), context);
94 if(null != urlConn) {
95 final TextureData texData = TextureIO.newTextureData(GLProfile.getGL2ES2(), urlConn.getInputStream(), false, imageSuffix);
96 final Texture tex = new Texture(getTextureTarget());
97 tex.updateImage(gl, texData);
98 addFrame(gl, tex);
99 }
100 }
101 public final int getFrameCount() { return frames.size(); }
102 public final int getCurrentIdx() { return frameIdx; }
103 public final void setCurrentIdx(final int idx) throws IndexOutOfBoundsException {
104 if( 0 > idx || idx >= frames.size() ) {
105 throw new IndexOutOfBoundsException("idx shall be within 0 <= "+idx+" < "+frames.size());
106 }
107 frameIdx=idx;
108 }
109 public final void setManualStepping(final boolean v) { manualStepping = v; }
110 public final boolean isManualStepping() { return manualStepping; }
111
112 /** Returns {@code true} if not {@link #isManualStepping()} and {@link #getFrameCount()} > 1 */
113 public final boolean isSequenceAnimating() { return !manualStepping && frames.size() > 1; }
114 public final TextureSequence.TextureFrame getFrame(final int idx) { return frames.get(idx); }
115
116 public void destroy(final GL gl) throws GLException {
117 for(int i=frames.size()-1; i>=0; i--) {
118 frames.get(i).getTexture().destroy(gl);
119 }
120 frames.clear();
121 }
122
123 @Override
124 public int getTextureTarget() {
125 return GL.GL_TEXTURE_2D;
126 }
127
128 @Override
129 public int getTextureUnit() {
130 return textureUnit;
131 }
132
133 @Override
134 public int[] getTextureMinMagFilter() {
135 return texMinMagFilter;
136 }
137
138 @Override
139 public int[] getTextureWrapST() {
140 return texWrapST;
141 }
142
143 /**
144 * {@inheritDoc}
145 * <p>
146 * Defaults to {@code true} and toggling is supported via {@link #setARatioAdjustment(boolean)}
147 * </p>
148 */
149 @Override
150 public boolean useARatioAdjustment() { return aRatioAdjustment; }
151
152 /**
153 * {@inheritDoc}
154 * <p>
155 * Defaults to {@code true}.
156 * </p>
157 */
158 @Override
159 public void setARatioAdjustment(final boolean v) { aRatioAdjustment = v; }
160
161 /**
162 * {@inheritDoc}
163 * <p>
164 * Defaults to {@code false} and toggling is supported via {@link #setARatioLetterbox(boolean, Vec4f)}
165 * </p>
166 */
167 @Override
168 public boolean useARatioLetterbox() { return aRatioLbox; }
169
170 @Override
171 public Vec4f getARatioLetterboxBackColor() { return aRatioLboxBackColor; }
172
173 /**
174 * {@inheritDoc}
175 * <p>
176 * Defaults to {@code false}.
177 * </p>
178 */
179 @Override
180 public void setARatioLetterbox(final boolean v, final Vec4f backColor) {
181 aRatioLbox = v;
182 if( null != backColor ) {
183 aRatioLboxBackColor.set(backColor);
184 }
185 };
186
187 @Override
188 public boolean isTextureAvailable() { return frames.size() > 0; }
189
190 @Override
191 public TextureSequence.TextureFrame getLastTexture() throws IllegalStateException {
192 return frames.get(frameIdx); // may return null
193 }
194
195 @Override
196 public TextureSequence.TextureFrame getNextTexture(final GL gl) throws IllegalStateException {
197 if( !manualStepping ) {
198 frameIdx = ( frameIdx + 1 ) % frames.size();
199 }
200 return frames.get(frameIdx);
201 }
202
203 @Override
204 public String getRequiredExtensionsShaderStub() throws IllegalStateException {
205 return "// TextTextureSequence: No extensions required\n";
206 }
207
208 @Override
209 public String getTextureSampler2DType() throws IllegalStateException {
210 return "sampler2D" ;
211 }
212
213 private String textureLookupFunctionName = "myTexture2D";
214
215 @Override
216 public String setTextureLookupFunctionName(final String texLookupFuncName) throws IllegalStateException {
217 if(useBuildInTexLookup) {
218 textureLookupFunctionName = "texture2D";
219 } else if(null != texLookupFuncName && texLookupFuncName.length()>0) {
220 textureLookupFunctionName = texLookupFuncName;
221 }
222 textureFragmentShaderHashCode = 0;
223 return textureLookupFunctionName;
224 }
225
226 @Override
227 public String getTextureLookupFunctionName() throws IllegalStateException {
228 return textureLookupFunctionName;
229 }
230
231 @Override
232 public String getTextureLookupFragmentShaderImpl() throws IllegalStateException {
233 if(useBuildInTexLookup) {
234 return "";
235 }
236 return
237 "\n"+
238 "vec4 "+textureLookupFunctionName+"(in "+getTextureSampler2DType()+" image, in vec2 texCoord) {\n"+
239 " return texture2D(image, texCoord);\n"+
240 "}\n\n";
241 }
242
243 @Override
245 // return getTextureSampler2DType()+";"+getTextureLookupFunctionName()+";"+getTextureLookupFragmentShaderImpl();
246 if( useBuildInTexLookup ) {
248 } else {
250 }
251 }
252
253 @Override
255 if( !isTextureAvailable() ) {
256 textureFragmentShaderHashCode = 0;
257 return 0;
258 } else if( 0 == textureFragmentShaderHashCode ) {
259 final int hash = getTextureFragmentShaderHashID().hashCode();
260 textureFragmentShaderHashCode = hash;
261 }
262 return textureFragmentShaderHashCode;
263 }
264}
4D Vector based upon four float components.
Definition: Vec4f.java:37
Vec4f set(final Vec4f o)
this = o, returns this.
Definition: Vec4f.java:67
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
Simple TextureSequence implementation allowing existing textures or image streams to be used and repl...
int getTextureTarget()
Returns the texture target used by implementation.
TextureSequence.TextureFrame getNextTexture(final GL gl)
Returns the next texture to be rendered.
String getRequiredExtensionsShaderStub()
In case a shader extension is required, based on the implementation and the runtime GL profile,...
final void addFrame(final GL gl, final Class<?> context, final String imageResourcePath, final String imageSuffix)
TextureSequence.TextureFrame getLastTexture()
Returns the last updated texture.
String getTextureLookupFragmentShaderImpl()
Returns the complete texture2D lookup function code of type.
Vec4f getARatioLetterboxBackColor()
Returns useARatioLetterbox() background color for added letter-box space, defaults to transparent zer...
final TextureSequence.TextureFrame addFrame(final GL gl, final Texture tex)
String getTextureSampler2DType()
Returns either sampler2D or samplerExternalOES depending on getLastTexture().
final boolean isSequenceAnimating()
Returns true if not isManualStepping() and getFrameCount() > 1.
String setTextureLookupFunctionName(final String texLookupFuncName)
Set the desired shader code's texture lookup function name.
boolean useARatioAdjustment()
Returning true indicates texture correction for aspect-ratio in the shader.Graph's Region shader will...
void setParams(final int magFilter, final int minFilter, final int wrapS, final int wrapT)
final TextureSequence.TextureFrame addFrame(final GL gl, final TextureSequence.TextureFrame frame)
void setARatioAdjustment(final boolean v)
Toggles useARatioLetterbox().Default value is implementation specific and toggling is optional....
boolean removeFrame(final TextureFrame tex)
int getTextureUnit()
Return the texture unit used to render the current frame.
final void setManualStepping(final boolean v)
void setARatioLetterbox(final boolean v, final Vec4f backColor)
Toggles useARatioLetterbox().Default value is implementation specific and toggling is optional....
final TextureSequence.TextureFrame getFrame(final int idx)
String getTextureLookupFunctionName()
Returns the chosen lookup function name, which can be set via setTextureLookupFunctionName(String).
int getTextureFragmentShaderHashCode()
Returns the hash code of the string getTextureFragmentShaderHashID().
boolean useARatioLetterbox()
Returns whether useARatioAdjustment() shall add letter-box space to match aspect-ratio,...
ImageSequence(final int textureUnit, final boolean useBuildInTexLookup)
String getTextureFragmentShaderHashID()
Returns the concatenated string representing the following values utilized for getTextureFragmentShad...
boolean isTextureAvailable()
Returns true if texture source is ready and a texture is available via getNextTexture(GL) and getLast...
Represents the data for an OpenGL texture.
static TextureData newTextureData(final GLProfile glp, final File file, final boolean mipmap, String fileSuffix)
Creates a TextureData from the given file.
Definition: TextureIO.java:233
Texture holder interface, maybe specialized by implementation to associated related data.
Represents an OpenGL texture object.
Definition: Texture.java:173
void updateImage(final GL gl, final TextureData data)
Updates the entire content area incl.
Definition: Texture.java:524
static final int GL_TEXTURE_MAG_FILTER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_MAG_FILTER" w...
Definition: GL.java:197
static final int GL_TEXTURE_2D
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_2D" with expr...
Definition: GL.java:491
static final int GL_TEXTURE_WRAP_S
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_WRAP_S" with ...
Definition: GL.java:485
static final int GL_TEXTURE_MIN_FILTER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_MIN_FILTER" w...
Definition: GL.java:372
static final int GL_TEXTURE_WRAP_T
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_WRAP_T" with ...
Definition: GL.java:489
void glTexParameteri(int target, int pname, int param)
Entry point to C language function: void {@native glTexParameteri}(GLenum target,...
static final int GL_NEAREST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NEAREST" with express...
Definition: GL.java:715
static final int GL_CLAMP_TO_EDGE
GL_ES_VERSION_2_0, GL_VERSION_1_2, GL_VERSION_ES_1_0, GL_SGIS_texture_edge_clamp Alias for: GL_CLAMP_...
Definition: GL.java:775
Protocol for texture sequences, like animations, movies, etc.