JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Teapot.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.junit.jogl.demos.gl2;
2
3import java.net.URLConnection;
4
5import com.jogamp.opengl.GL;
6import com.jogamp.opengl.GL2;
7import com.jogamp.opengl.GLAutoDrawable;
8import com.jogamp.opengl.GLEventListener;
9import com.jogamp.opengl.fixedfunc.GLLightingFunc;
10import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
11
12import com.jogamp.common.util.IOUtil;
13import com.jogamp.opengl.test.junit.jogl.util.texture.PNGTstFiles;
14import com.jogamp.opengl.util.gl2.GLUT;
15import com.jogamp.opengl.util.texture.Texture;
16import com.jogamp.opengl.util.texture.TextureIO;
17
18/**
19 * Adapted from
20 * http://www.java-tips.org/other-api-tips/jogl/how-to-draw-a-texture-mapped-teapot-with-automatically-generated-texture-coordi.html
21 */
22public class Teapot implements GLEventListener {
23
24 private GLUT glut;
25
26 /* glTexGen stuff: */
27 // private final float sgenparams[] = { 1.0f, 1.0f, 1.0f, 0.0f };
28
29 private Texture tex = null;
30
31 private void enableStates(final GL2 gl, final boolean enable) {
32 if( enable ) {
33 if( null != tex ) {
34 tex.bind(gl);
35 }
37 gl.glDepthFunc(GL.GL_LESS); // default
38 // gl.glEnable(GL2.GL_TEXTURE_GEN_S);
39 // gl.glEnable(GL2.GL_TEXTURE_1D);
47 gl.glCullFace(GL.GL_BACK); // default
50 } else {
51 if( null != tex ) {
52 gl.glBindTexture(tex.getTarget(), 0);
53 }
55 // gl.glDisable(GL2.GL_TEXTURE_GEN_S);
56 // gl.glDisable(GL2.GL_TEXTURE_1D);
63 gl.glFrontFace(GL.GL_CCW); // default
64 }
65 }
66
67 @Override
68 public void init(final GLAutoDrawable drawable) {
69 final GL2 gl = drawable.getGL().getGL2();
70 glut = new GLUT();
71
72 gl.glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
73
74 try {
75 final URLConnection urlConn = IOUtil.getResource("test-ntscP_3-01-160x90.png", PNGTstFiles.class.getClassLoader(), PNGTstFiles.class);
76 tex = TextureIO.newTexture(gl, TextureIO.newTextureData(gl.getGLProfile(), urlConn.getInputStream(), false, TextureIO.PNG));
77 } catch (final Exception e) {
78 e.printStackTrace();
79 }
80 // tex.bind(gl);
81
82 // uncomment this and comment the above to see a working texture
83 // makeStripeImage();
84 // gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
85 // gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE,
86 // GL2.GL_MODULATE);
87 // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_S,
88 // GL.GL_REPEAT);
89 // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER,
90 // GL.GL_LINEAR);
91 // gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL.GL_TEXTURE_MIN_FILTER,
92 // GL.GL_LINEAR);
93 // gl.glTexImage1D(GL2.GL_TEXTURE_1D, 0, 3, stripeImageWidth, 0,
94 // GL.GL_RGB, GL.GL_UNSIGNED_BYTE, stripeImageBuf);
95
96 // gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
97
98 // gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
99 // gl.glTexGenfv(GL2.GL_S, GL2.GL_OBJECT_PLANE, sgenparams, 0);
100 }
101
102 float angleZ = 0.0f;
103 float rotDir = 1.0f;
104 public float rotIncr = 0.4f;
105
106 @Override
107 public void display(final GLAutoDrawable gLDrawable) {
108 final GL2 gl = gLDrawable.getGL().getGL2();
109
110 enableStates(gl, true);
111
113 gl.glPushMatrix();
114 gl.glRotatef(angleZ, 0.0f, 1.0f, 0.0f);
115 gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
116 glut.glutSolidTeapot(2.0f);
117 gl.glPopMatrix();
118 gl.glFlush();
119 if( angleZ >= 180.0f ) {
120 rotDir = -1.0f;
121 } else if (angleZ <= 0.0f ) {
122 rotDir = +1.0f;
123 }
124 angleZ += rotIncr * rotDir;
125
126 enableStates(gl, false);
127 }
128
129 @Override
130 public void reshape(final GLAutoDrawable gLDrawable, final int x, final int y, final int w, final int h) {
131 final GL2 gl = gLDrawable.getGL().getGL2();
132
134 gl.glLoadIdentity();
135 if (w <= h) {
136 gl.glOrtho(-3.5, 3.5, -3.5 * h / w,
137 3.5 * h / w, -3.5, 3.5);
138 } else {
139 gl.glOrtho(-3.5 * w / h,
140 3.5 * w / h, -3.5, 3.5, -3.5, 3.5);
141 }
143 gl.glLoadIdentity();
144 }
145
146 @Override
147 public void dispose(final GLAutoDrawable gLDrawable) {
148 }
149}
Adapted from http://www.java-tips.org/other-api-tips/jogl/how-to-draw-a-texture-mapped-teapot-with-au...
Definition: Teapot.java:22
void dispose(final GLAutoDrawable gLDrawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Definition: Teapot.java:147
void display(final GLAutoDrawable gLDrawable)
Called by the drawable to initiate OpenGL rendering by the client.
Definition: Teapot.java:107
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Definition: Teapot.java:68
void reshape(final GLAutoDrawable gLDrawable, final int x, final int y, final int w, final int h)
Called by the drawable during the first repaint after the component has been resized.
Definition: Teapot.java:130
Subset of the routines provided by the GLUT interface.
Definition: GLUT.java:96
void glutSolidTeapot(final double scale)
Renders the teapot as a solid shape of the specified size.
Definition: GLUT.java:270
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
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
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
void bind(final GL gl)
Binds this texture to the given GL context.
Definition: Texture.java:377
int getTarget()
Returns the OpenGL "target" of this texture.
Definition: Texture.java:400
void glOrtho(double left, double right, double bottom, double top, double near_val, double far_val)
static final int GL_AUTO_NORMAL
GL_VERSION_1_0 Define "GL_AUTO_NORMAL" with expression '0x0D80', CType: int
Definition: GL2.java:587
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
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_BACK
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_BACK" with expression...
Definition: GL.java:330
static final int GL_REPEAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_REPEAT" with expressi...
Definition: GL.java:660
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
void glBindTexture(int target, int texture)
Entry point to C language function: void {@native glBindTexture}(GLenum target, GLuint texture) Pa...
void glDisable(int cap)
Entry point to C language function: void {@native glDisable}(GLenum cap) Part of GL_ES_VERSION_2_0...
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_CCW
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CCW" with expression ...
Definition: GL.java:611
static final int GL_CW
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CW" with expression '...
Definition: GL.java:467
static final int GL_FRONT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FRONT" with expressio...
Definition: GL.java:597
void glFrontFace(int mode)
Entry point to C language function: void {@native glFrontFace}(GLenum mode) Part of GL_ES_VERSION_...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
void glTexParameterf(int target, int pname, float param)
Entry point to C language function: void {@native glTexParameterf}(GLenum target,...
static final int GL_LESS
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LESS" with expression...
Definition: GL.java:134
static final int GL_CULL_FACE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CULL_FACE" with expre...
Definition: GL.java:720
void glFlush()
Entry point to C language function: void {@native glFlush}() Part of GL_ES_VERSION_2_0,...
void glCullFace(int mode)
Entry point to C language function: void {@native glCullFace}(GLenum mode) Part of GL_ES_VERSION_2...
void glDepthFunc(int func)
Entry point to C language function: void {@native glDepthFunc}(GLenum func) Part of GL_ES_VERSION_...
void glMaterialf(int face, int pname, float param)
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
void glPopMatrix()
Pop the current matrix from it's stack.
void glRotatef(float angle, float x, float y, float z)
Rotate the current matrix.
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.