JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TextureDraw01GL2Listener.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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 */
28
29package com.jogamp.opengl.test.junit.jogl.demos.gl2;
30
31import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
32import com.jogamp.opengl.util.texture.Texture;
33import com.jogamp.opengl.util.texture.TextureCoords;
34import com.jogamp.opengl.util.texture.TextureData;
35import com.jogamp.opengl.util.texture.TextureIO;
36
37import com.jogamp.opengl.GL;
38import com.jogamp.opengl.GL2ES1;
39import com.jogamp.opengl.GL2;
40import com.jogamp.opengl.GL2GL3;
41import com.jogamp.opengl.GLAutoDrawable;
42import com.jogamp.opengl.GLEventListener;
43import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
44import com.jogamp.opengl.glu.GLU;
45
47 private final GLU glu = new GLU();
48 private final TextureData textureData;
49 private Texture texture;
50 boolean keepTextureBound;
51
53 this.textureData = td;
54 this.keepTextureBound = false;
55 }
56
57 @Override
58 public void setKeepTextureBound(final boolean v) {
59 this.keepTextureBound = v;
60 }
61 @Override
62 public Texture getTexture( ) {
63 return this.texture;
64 }
65
66 @Override
67 public void init(final GLAutoDrawable drawable) {
68 final GL2 gl = drawable.getGL().getGL2();
69 if(null!=textureData) {
70 this.texture = TextureIO.newTexture(drawable.getGL(), textureData);
71 if( keepTextureBound ) {
72 texture.enable(gl);
73 texture.bind(gl);
74 }
75 }
76 }
77
78 @Override
79 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
80 final GL2 gl = drawable.getGL().getGL2();
82 gl.glLoadIdentity();
83 glu.gluOrtho2D(0, 1, 0, 1);
85 gl.glLoadIdentity();
86 }
87
88 @Override
89 public void dispose(final GLAutoDrawable drawable) {
90 final GL2 gl = drawable.getGL().getGL2();
91 if(null!=texture) {
92 texture.disable(gl);
93 texture.destroy(gl);
94 }
95 if(null!=textureData) {
96 textureData.destroy();
97 }
98 }
99
100 @Override
101 public void display(final GLAutoDrawable drawable) {
102 final GL2 gl = drawable.getGL().getGL2();
103
104 // draw one quad with the texture
105 if(null!=texture) {
106 if( !keepTextureBound ) {
107 texture.enable(gl);
108 texture.bind(gl);
109 }
111 final TextureCoords coords = texture.getImageTexCoords();
113 gl.glTexCoord2f(coords.left(), coords.bottom());
114 gl.glVertex3f(0, 0, 0);
115 gl.glTexCoord2f(coords.right(), coords.bottom());
116 gl.glVertex3f(1, 0, 0);
117 gl.glTexCoord2f(coords.right(), coords.top());
118 gl.glVertex3f(1, 1, 0);
119 gl.glTexCoord2f(coords.left(), coords.top());
120 gl.glVertex3f(0, 1, 0);
121 gl.glEnd();
122 if( !keepTextureBound ) {
123 texture.disable(gl);
124 }
125 }
126 }
127}
128
Provides access to the OpenGL Utility Library (GLU).
Definition: GLU.java:43
void gluOrtho2D(float left, float right, float bottom, float top)
Definition: GLU.java:1358
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Specifies texture coordinates for a rectangular area of a texture.
Represents the data for an OpenGL texture.
static Texture newTexture(final TextureData data)
Creates an OpenGL texture object from the specified TextureData using the current OpenGL context.
Definition: TextureIO.java:459
Represents an OpenGL texture object.
Definition: Texture.java:173
TextureCoords getImageTexCoords()
Returns the set of texture coordinates corresponding to the entire image.
Definition: Texture.java:480
void bind(final GL gl)
Binds this texture to the given GL context.
Definition: Texture.java:377
void disable(final GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the given GL state.
Definition: Texture.java:357
void enable(final GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the given GL context's state.
Definition: Texture.java:330
void destroy(final GL gl)
Destroys and nulls the underlying native texture used by this Texture instance if owned,...
Definition: Texture.java:388
static final int GL_TEXTURE_ENV_MODE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV_MODE" with expression '0x2200',...
Definition: GL2ES1.java:202
void glTexEnvi(int target, int pname, int param)
Entry point to C language function: void {@native glTexEnvi}(GLenum target, GLenum pname,...
static final int GL_TEXTURE_ENV
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV" with expression '0x2300',...
Definition: GL2ES1.java:154
static final int GL_QUADS
GL_ES_VERSION_3_2, GL_VERSION_1_1, GL_VERSION_1_0, GL_OES_tessellation_shader, GL_EXT_tessellation_sh...
Definition: GL2ES3.java:734
void glTexCoord2f(float s, float t)
Entry point to C language function: void {@native glTexCoord2f}(GLfloat s, GLfloat t) Part of GL_V...
void glBegin(int mode)
Entry point to C language function: void {@native glBegin}(GLenum mode) Part of GL_VERSION_1_0
void glVertex3f(float x, float y, float z)
Entry point to C language function: void {@native glVertex3f}(GLfloat x, GLfloat y,...
void glEnd()
Entry point to C language function: void {@native glEnd}() Part of GL_VERSION_1_0
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2 getGL2()
Casts this object to the GL2 interface.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_REPLACE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0, GL_EXT_texture Alias for: GL_RE...
Definition: GL.java:236
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.
void glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.