summaryrefslogtreecommitdiffstats
path: root/src/demos
diff options
context:
space:
mode:
authorKenneth Russel <[email protected]>2006-01-07 23:47:49 +0000
committerKenneth Russel <[email protected]>2006-01-07 23:47:49 +0000
commit2e32df6eb0181d50f002e0dfe055572b60088428 (patch)
tree6498fa09e48621079a6d6885c8c7c3acf8347d66 /src/demos
parentecb75532f0c488031d49935d1a7116be07544270 (diff)
Reorganized and added code in TextureIO and Texture classes to support
cubemap generation as well as complete replacement of the texture's image, not just a sub-image. Added setTexParameteri to provide control over things like the GL_TEXTURE_WRAP_ modes. Ported JOGL demos to use new TextureIO class instead of TGAImage and ImageIO directly. Fixed longstanding bug in ExaminerViewer's setting up of inverse rotation causing errors in loading of uffizi cubemap. git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/jogl-demos/trunk@163 3298f667-5e0e-4b4a-8ed4-a3559d26a5f4
Diffstat (limited to 'src/demos')
-rw-r--r--src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java100
-rw-r--r--src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java4
-rw-r--r--src/demos/proceduralTexturePhysics/Water.java194
-rwxr-xr-xsrc/demos/texture/TestTexture.java4
-rwxr-xr-xsrc/demos/util/Cubemap.java77
-rw-r--r--src/demos/vertexProgRefract/VertexProgRefract.java83
6 files changed, 188 insertions, 274 deletions
diff --git a/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java b/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java
index c8395ff..02e3c5c 100644
--- a/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java
+++ b/src/demos/hwShadowmapsSimple/HWShadowmapsSimple.java
@@ -128,8 +128,8 @@ public class HWShadowmapsSimple extends Demo {
// Texture objects
private static final int TEX_SIZE = 1024;
- private int decal;
- private int light_image;
+ private Texture decal;
+ private Texture light_image;
private int light_view_depth;
// Depth buffer format
@@ -194,21 +194,18 @@ public class HWShadowmapsSimple extends Demo {
gl.glClearColor(.5f, .5f, .5f, .5f);
- decal = genTexture(gl);
- gl.glBindTexture(GL.GL_TEXTURE_2D, decal);
- BufferedImage img = readPNGImage("demos/data/images/decal_image.png");
- makeRGBTexture(gl, img, GL.GL_TEXTURE_2D, true);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
-
- light_image = genTexture(gl);
- gl.glBindTexture(GL.GL_TEXTURE_2D, light_image);
- img = readPNGImage("demos/data/images/nvlogo_spot.png");
- makeRGBTexture(gl, img, GL.GL_TEXTURE_2D, true);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
- gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
+ try {
+ decal = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/decal_image.png"),
+ true,
+ TextureIO.PNG);
+ decal.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
+ decal.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
+ light_image = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream("demos/data/images/nvlogo_spot.png"),
+ true,
+ TextureIO.PNG);
+ } catch (IOException e) {
+ throw new GLException(e);
+ }
quad = gl.glGenLists(1);
gl.glNewList(quad, GL.GL_COMPILE);
@@ -463,51 +460,6 @@ public class HWShadowmapsSimple extends Demo {
return tmp[0];
}
- private BufferedImage readPNGImage(String resourceName) {
- try {
- // Note: use of BufferedInputStream works around 4764639/4892246
- BufferedImage img = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(resourceName)));
- if (img == null) {
- throw new RuntimeException("Error reading resource " + resourceName);
- }
- return img;
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- private void makeRGBTexture(GL gl, BufferedImage img, int target, boolean mipmapped) {
- switch (img.getType()) {
- case BufferedImage.TYPE_3BYTE_BGR:
- case BufferedImage.TYPE_CUSTOM: {
- byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
- if (mipmapped) {
- glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB,
- GL.GL_UNSIGNED_BYTE, ByteBuffer.wrap(data));
- } else {
- gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0,
- GL.GL_RGB, GL.GL_UNSIGNED_BYTE, ByteBuffer.wrap(data));
- }
- break;
- }
-
- case BufferedImage.TYPE_INT_RGB: {
- int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
- if (mipmapped) {
- glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB,
- GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(data));
- } else {
- gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0,
- GL.GL_RGB, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(data));
- }
- break;
- }
-
- default:
- throw new RuntimeException("Unsupported image type " + img.getType());
- }
- }
-
private void eye_linear_texgen(GL gl) {
Mat4f m = new Mat4f();
m.makeIdent();
@@ -576,10 +528,10 @@ public class HWShadowmapsSimple extends Demo {
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glDisable(GL.GL_LIGHTING);
- gl.glBindTexture(GL.GL_TEXTURE_2D, decal);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ decal.bind();
+ decal.enable();
gl.glCallList(quad);
- gl.glDisable(GL.GL_TEXTURE_2D);
+ decal.disable();
gl.glEnable(GL.GL_LIGHTING);
texgen(gl, false);
@@ -650,8 +602,8 @@ public class HWShadowmapsSimple extends Demo {
applyTransform(gl, spotlightInverseTransform);
gl.glMatrixMode(GL.GL_MODELVIEW);
- gl.glBindTexture(GL.GL_TEXTURE_2D, light_image);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ light_image.bind();
+ light_image.enable();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
gl.glActiveTexture(GL.GL_TEXTURE0);
@@ -663,7 +615,7 @@ public class HWShadowmapsSimple extends Demo {
render_scene(gl, cameraTransform, drawable, params);
gl.glActiveTexture(GL.GL_TEXTURE1);
- gl.glDisable(GL.GL_TEXTURE_2D);
+ light_image.disable();
gl.glActiveTexture(GL.GL_TEXTURE0);
render_manipulators(gl, cameraTransform, drawable, params);
@@ -697,8 +649,8 @@ public class HWShadowmapsSimple extends Demo {
applyTransform(gl, spotlightInverseTransform);
gl.glMatrixMode(GL.GL_MODELVIEW);
- gl.glBindTexture(GL.GL_TEXTURE_2D, light_image);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ light_image.bind();
+ light_image.enable();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
// depth compare
@@ -732,7 +684,7 @@ public class HWShadowmapsSimple extends Demo {
render_scene(gl, cameraTransform, drawable, params);
gl.glActiveTexture(GL.GL_TEXTURE1);
- gl.glDisable(GL.GL_TEXTURE_2D);
+ light_image.disable();
gl.glActiveTexture(GL.GL_TEXTURE2);
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glActiveTexture(GL.GL_TEXTURE0);
@@ -772,8 +724,8 @@ public class HWShadowmapsSimple extends Demo {
glu.gluPerspective(lightshaper_fovy, 1, lightshaper_zNear, lightshaper_zFar);
gl.glMatrixMode(GL.GL_MODELVIEW);
- gl.glBindTexture(GL.GL_TEXTURE_2D, light_image);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ light_image.bind();
+ light_image.enable();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
gl.glActiveTexture(GL.GL_TEXTURE0);
@@ -788,7 +740,7 @@ public class HWShadowmapsSimple extends Demo {
render_scene(gl, spotlightTransform, null, null);
gl.glActiveTexture(GL.GL_TEXTURE1);
- gl.glDisable(GL.GL_TEXTURE_2D);
+ light_image.disable();
gl.glActiveTexture(GL.GL_TEXTURE0);
}
diff --git a/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java b/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java
index 6fe2369..6a7d116 100644
--- a/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java
+++ b/src/demos/proceduralTexturePhysics/ProceduralTexturePhysics.java
@@ -94,6 +94,7 @@ public class ProceduralTexturePhysics extends Demo {
//
public void shutdownDemo() {
+ viewer.detach();
ManipManager.getManipManager().unregisterWindow(drawable);
drawable.removeGLEventListener(this);
super.shutdownDemo();
@@ -124,7 +125,8 @@ public class ProceduralTexturePhysics extends Demo {
water.initialize("demos/data/images/nvfixed.tga",
"demos/data/images/nvspin.tga",
"demos/data/images/droplet.tga",
- "demos/data/cubemaps/CloudyHills_{0}.tga",
+ "demos/data/cubemaps/CloudyHills_",
+ "tga",
drawable);
GL gl = drawable.getGL();
diff --git a/src/demos/proceduralTexturePhysics/Water.java b/src/demos/proceduralTexturePhysics/Water.java
index f29d4a1..2fd0f0b 100644
--- a/src/demos/proceduralTexturePhysics/Water.java
+++ b/src/demos/proceduralTexturePhysics/Water.java
@@ -74,22 +74,16 @@ public class Water {
public static final int CA_DO_NOT_RENDER = 5;
private int[] initialMapDimensions = new int[2];
- private TGAImage initialMap;
+ private TextureData initialMapData;
private String tmpSpinFilename;
private String tmpDropletFilename;
- private String tmpCubeMapFilenamePattern;
+ private String tmpCubeMapFilenamePrefix;
+ private String tmpCubeMapFilenameSuffix;
private GLPbuffer pbuffer;
private Rotf cameraOrientation = new Rotf();
- // Static texture names
- private static final int CA_TEXTURE_INITIAL_MAP = 0;
- private static final int CA_TEXTURE_SPIN = 1;
- private static final int CA_TEXTURE_DROPLET = 2;
- private static final int CA_TEXTURE_CUBEMAP = 3;
- private static final int CA_NUM_STATIC_TEXTURES = 4;
-
// Dynamic texture names
private static final int CA_TEXTURE_FORCE_INTERMEDIATE = 0;
private static final int CA_TEXTURE_FORCE_TARGET = 1;
@@ -111,8 +105,13 @@ public class Water {
private static final int CA_DRAW_SCREEN_QUAD = 7;
private static final int CA_NUM_LISTS = 8;
- private int[] staticTextureIDs = new int[CA_NUM_STATIC_TEXTURES];
- private int[] dynamicTextureIDs = new int[CA_NUM_DYNAMIC_TEXTURES];
+ // Static textures
+ private Texture initialMapTex;
+ private Texture spinTex;
+ private Texture dropletTex;
+ private Texture cubemap;
+
+ private Texture[] dynamicTextures = new Texture[CA_NUM_DYNAMIC_TEXTURES];
private int texHeightInput; // current input height texture ID.
private int texHeightOutput; // current output height texture ID.
@@ -190,12 +189,14 @@ public class Water {
public void initialize(String initialMapFilename,
String spinFilename,
String dropletFilename,
- String cubeMapFilenamePattern,
+ String cubeMapFilenamePrefix,
+ String cubeMapFilenameSuffix,
GLAutoDrawable parentWindow) {
loadInitialTexture(initialMapFilename);
tmpSpinFilename = spinFilename;
tmpDropletFilename = dropletFilename;
- tmpCubeMapFilenamePattern = cubeMapFilenamePattern;
+ tmpCubeMapFilenamePrefix = cubeMapFilenamePrefix;
+ tmpCubeMapFilenameSuffix = cubeMapFilenameSuffix;
// create the pbuffer. Will use this as an offscreen rendering buffer.
// it allows rendering a texture larger than our window.
@@ -258,11 +259,11 @@ public class Water {
// Draw quad over full display
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_NORMAL_MAP]);
- gl.glDisable(GL.GL_TEXTURE_2D);
+ dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
+ dynamicTextures[CA_TEXTURE_NORMAL_MAP].disable();
gl.glActiveTexture(GL.GL_TEXTURE3);
- gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, staticTextureIDs[CA_TEXTURE_CUBEMAP]);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ cubemap.bind();
+ cubemap.enable();
gl.glColor4f(1, 1, 1, 1);
gl.glBegin(GL.GL_QUADS);
@@ -293,6 +294,7 @@ public class Water {
gl.glEnd();
+ cubemap.disable();
gl.glDisable(GL.GL_FRAGMENT_PROGRAM_ARB);
break;
@@ -301,7 +303,7 @@ public class Water {
case CA_FULLSCREEN_NORMALMAP: {
// Draw quad over full display
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_NORMAL_MAP]);
+ dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
break;
@@ -319,7 +321,7 @@ public class Water {
case CA_FULLSCREEN_FORCE: {
// Draw quad over full display
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_FORCE_TARGET]);
+ dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
gl.glCallList(displayListIDs[CA_DRAW_SCREEN_QUAD]);
break;
@@ -329,7 +331,7 @@ public class Water {
// Draw quad over full display
// lower left
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_FORCE_TARGET]);
+ dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
@@ -348,7 +350,7 @@ public class Water {
gl.glPopMatrix();
// upper left
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_NORMAL_MAP]);
+ dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
@@ -471,31 +473,30 @@ public class Water {
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
}
- private TGAImage loadImage(String resourceName) {
- try {
- return TGAImage.read(getClass().getClassLoader().getResourceAsStream(resourceName));
- } catch (IOException e) {
- throw new GLException(e);
- }
- }
-
// We need to load the initial texture file early to get the width
// and height for the pbuffer
private void loadInitialTexture(String initialMapFilename) {
try {
- initialMap = TGAImage.read(getClass().getClassLoader().getResourceAsStream(initialMapFilename));
+ initialMapData = TextureIO.newTextureData(getClass().getClassLoader().getResourceAsStream(initialMapFilename),
+ false,
+ TextureIO.getFileSuffix(initialMapFilename));
} catch (IOException e) {
throw new GLException(e);
}
- initialMapDimensions[0] = initialMap.getWidth();
- initialMapDimensions[1] = initialMap.getHeight();
+ initialMapDimensions[0] = initialMapData.getWidth();
+ initialMapDimensions[1] = initialMapData.getHeight();
}
private void initOpenGL(GL gl) {
- loadTextures(gl, tmpSpinFilename, tmpDropletFilename, tmpCubeMapFilenamePattern);
+ try {
+ loadTextures(gl, tmpSpinFilename, tmpDropletFilename, tmpCubeMapFilenamePrefix, tmpCubeMapFilenameSuffix);
+ } catch (IOException e) {
+ throw new GLException(e);
+ }
tmpSpinFilename = null;
tmpDropletFilename = null;
- tmpCubeMapFilenamePattern = null;
+ tmpCubeMapFilenamePrefix = null;
+ tmpCubeMapFilenameSuffix = null;
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
@@ -674,11 +675,11 @@ public class Water {
switch (flipState) {
case 0:
- texHeightInput = dynamicTextureIDs[CA_TEXTURE_HEIGHT_SOURCE]; // initial height map.
- texHeightOutput = dynamicTextureIDs[CA_TEXTURE_HEIGHT_TARGET]; // next height map.
+ texHeightInput = dynamicTextures[CA_TEXTURE_HEIGHT_SOURCE].getTextureObject(); // initial height map.
+ texHeightOutput = dynamicTextures[CA_TEXTURE_HEIGHT_TARGET].getTextureObject(); // next height map.
- texVelocityInput = dynamicTextureIDs[CA_TEXTURE_VELOCITY_SOURCE]; // initial velocity.
- texVelocityOutput = dynamicTextureIDs[CA_TEXTURE_VELOCITY_TARGET]; // next velocity.
+ texVelocityInput = dynamicTextures[CA_TEXTURE_VELOCITY_SOURCE].getTextureObject(); // initial velocity.
+ texVelocityOutput = dynamicTextures[CA_TEXTURE_VELOCITY_TARGET].getTextureObject(); // next velocity.
// Clear initial velocity texture to 0x80 == gray
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
@@ -756,7 +757,7 @@ public class Water {
// Now we need to copy the resulting pixels into the intermediate force field texture
gl.glActiveTexture(GL.GL_TEXTURE2);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_FORCE_INTERMEDIATE]);
+ dynamicTextures[CA_TEXTURE_FORCE_INTERMEDIATE].bind();
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
@@ -798,7 +799,7 @@ public class Water {
// Now we need to copy the resulting pixels into the intermediate force field texture
gl.glActiveTexture(GL.GL_TEXTURE1);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_FORCE_TARGET]);
+ dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
@@ -817,7 +818,7 @@ public class Water {
gl.glActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, texVelocityInput);
gl.glActiveTexture(GL.GL_TEXTURE1);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_FORCE_TARGET]);
+ dynamicTextures[CA_TEXTURE_FORCE_TARGET].bind();
gl.glActiveTexture(GL.GL_TEXTURE2);
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glActiveTexture(GL.GL_TEXTURE3);
@@ -965,7 +966,7 @@ public class Water {
// Now we need to copy the resulting pixels into the normal map
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, dynamicTextureIDs[CA_TEXTURE_NORMAL_MAP]);
+ dynamicTextures[CA_TEXTURE_NORMAL_MAP].bind();
// use CopyTexSubImage for speed (even though we copy all of it) since we pre-allocated the texture
gl.glCopyTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, 0, 0, initialMapDimensions[0], initialMapDimensions[1]);
@@ -975,8 +976,8 @@ public class Water {
gl.glDisable(GL.GL_REGISTER_COMBINERS_NV);
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, staticTextureIDs[CA_TEXTURE_INITIAL_MAP]);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ initialMapTex.bind();
+ initialMapTex.enable();
gl.glEnable(GL.GL_ALPHA_TEST);
@@ -993,7 +994,7 @@ public class Water {
if (spinLogo) {
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, staticTextureIDs[CA_TEXTURE_SPIN]);
+ spinTex.bind();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glRotatef(angle, 0, 0, 1);
@@ -1008,100 +1009,39 @@ public class Water {
gl.glDisable(GL.GL_BLEND);
}
- private void createTextureObject(GL gl, int id, TGAImage image, boolean test) {
- // Fetch image data out of image
- gl.glBindTexture(GL.GL_TEXTURE_2D, id);
- gl.glTexImage2D (GL.GL_TEXTURE_2D,
- 0,
- GL.GL_RGBA8,
- image.getWidth(),
- image.getHeight(),
- 0,
- image.getGLFormat(),
- GL.GL_UNSIGNED_BYTE,
- ByteBuffer.wrap(image.getData()));
- gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
- gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
- gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
- gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
- }
-
- private void loadCubeMap(GL gl, int id, String filenamePattern, boolean mipmap) {
- int[] faces = new int[] { GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X,
- GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
- GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
- GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
- GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
- GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
- String[] faceNames = new String[] { "posx", "negx", "posy", "negy", "posz", "negz" };
-
- // create and bind a cubemap texture object
- gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, id);
-
- // enable automipmap generation if needed.
- gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_GENERATE_MIPMAP_SGIS, (mipmap ? 1 : 0));
-
- if (mipmap)
- gl.glTexParameterf(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
- else
- gl.glTexParameterf(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
- gl.glTexParameterf(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
- gl.glTexParameterf(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
- gl.glTexParameterf(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
-
- // load 6 faces.
- MessageFormat fmt = new MessageFormat(filenamePattern);
- for (int i = 0; i < 6; i++) {
- String filename = MessageFormat.format(filenamePattern, new String[] { faceNames[i] });
- TGAImage image = loadImage(filename);
- gl.glTexImage2D(faces[i],
- 0,
- GL.GL_RGBA8,
- image.getWidth(),
- image.getHeight(),
- 0,
- image.getGLFormat(),
- GL.GL_UNSIGNED_BYTE,
- ByteBuffer.wrap(image.getData()));
- }
- }
-
private void loadTextures(GL gl,
String spinFilename,
String dropletFilename,
- String cubeMapFilenamePattern) {
- if (initialMap == null) {
+ String cubeMapFilenamePrefix,
+ String cubeMapFilenameSuffix) throws IOException {
+ if (initialMapData == null) {
throw new GLException("Must call loadInitialTexture ahead of time");
}
- TGAImage spin = loadImage(spinFilename);
- TGAImage droplet = loadImage(dropletFilename);
-
- gl.glGenTextures(CA_NUM_STATIC_TEXTURES, staticTextureIDs, 0);
- gl.glGenTextures(CA_NUM_DYNAMIC_TEXTURES, dynamicTextureIDs, 0); // also create intermediate texture object
-
- // upload the initial map texture
- createTextureObject(gl, staticTextureIDs[CA_TEXTURE_INITIAL_MAP], initialMap, true);
-
- createTextureObject(gl, staticTextureIDs[CA_TEXTURE_SPIN], spin, true);
-
- createTextureObject(gl, staticTextureIDs[CA_TEXTURE_DROPLET], droplet, false);
+ initialMapTex = TextureIO.newTexture(initialMapData);
+ spinTex = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream(spinFilename), false,
+ TextureIO.getFileSuffix(spinFilename));
+ dropletTex = TextureIO.newTexture(getClass().getClassLoader().getResourceAsStream(dropletFilename), false,
+ TextureIO.getFileSuffix(dropletFilename));
// load the cubemap texture
- loadCubeMap(gl, staticTextureIDs[CA_TEXTURE_CUBEMAP], cubeMapFilenamePattern, true);
+ cubemap = Cubemap.loadFromStreams(getClass().getClassLoader(),
+ cubeMapFilenamePrefix,
+ cubeMapFilenameSuffix,
+ true);
+ // now create dummy intermediate textures from the initial map texture
for (int i = 0; i < CA_NUM_DYNAMIC_TEXTURES; i++) {
- // now create a dummy intermediate textures from the initial map texture
- createTextureObject(gl, dynamicTextureIDs[i], initialMap, false);
+ dynamicTextures[i] = TextureIO.newTexture(initialMapData);
}
- initialMap = null;
+ initialMapData = null;
- texHeightInput = staticTextureIDs [CA_TEXTURE_INITIAL_MAP]; // initial height map.
- texHeightOutput = dynamicTextureIDs[CA_TEXTURE_HEIGHT_TARGET]; // next height map.
+ texHeightInput = initialMapTex.getTextureObject(); // initial height map.
+ texHeightOutput = dynamicTextures[CA_TEXTURE_HEIGHT_TARGET].getTextureObject(); // next height map.
- texVelocityInput = dynamicTextureIDs[CA_TEXTURE_VELOCITY_SOURCE]; // initial velocity.
- texVelocityOutput = dynamicTextureIDs[CA_TEXTURE_VELOCITY_TARGET]; // next velocity.
+ texVelocityInput = dynamicTextures[CA_TEXTURE_VELOCITY_SOURCE].getTextureObject(); // initial velocity.
+ texVelocityOutput = dynamicTextures[CA_TEXTURE_VELOCITY_TARGET].getTextureObject(); // next velocity.
}
private void createAndWriteUVOffsets(GL gl, int width, int height) {
@@ -1196,8 +1136,8 @@ public class Water {
gl.glDisable(GL.GL_VERTEX_PROGRAM_ARB);
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_2D, staticTextureIDs[CA_TEXTURE_DROPLET]);
- gl.glEnable(GL.GL_TEXTURE_2D);
+ dropletTex.bind();
+ dropletTex.enable();
gl.glActiveTexture(GL.GL_TEXTURE1);
gl.glDisable(GL.GL_TEXTURE_2D);
diff --git a/src/demos/texture/TestTexture.java b/src/demos/texture/TestTexture.java
index cd71a3a..1ef0074 100755
--- a/src/demos/texture/TestTexture.java
+++ b/src/demos/texture/TestTexture.java
@@ -129,6 +129,8 @@ public class TestTexture implements GLEventListener {
}
public void init(GLAutoDrawable drawable) {
+ drawable.setGL(new DebugGL(drawable.getGL()));
+
GL gl = drawable.getGL();
gl.glClearColor(0, 0, 0, 0);
gl.glEnable(GL.GL_DEPTH_TEST);
@@ -180,8 +182,8 @@ public class TestTexture implements GLEventListener {
if (texture != null) {
texture.enable();
- gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
texture.bind();
+ gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
TextureCoords coords = texture.getImageTexCoords();
gl.glBegin(GL.GL_QUADS);
diff --git a/src/demos/util/Cubemap.java b/src/demos/util/Cubemap.java
new file mode 100755
index 0000000..fc7cd16
--- /dev/null
+++ b/src/demos/util/Cubemap.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * - Redistribution of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistribution in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any kind. ALL
+ * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
+ * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
+ * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
+ * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
+ * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
+ * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
+ * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
+ * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
+ * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that this software is not designed or intended for use
+ * in the design, construction, operation or maintenance of any nuclear
+ * facility.
+ *
+ * Sun gratefully acknowledges that this software was originally authored
+ * and developed by Kenneth Bradley Russell and Christopher John Kline.
+ */
+
+package demos.util;
+
+import java.io.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.utils.*;
+
+/** Helper class for loading cubemaps from a set of textures. */
+
+public class Cubemap {
+ private static final String[] suffixes = { "posx", "negx", "posy", "negy", "posz", "negz" };
+ private static final int[] targets = { GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X,
+ GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
+ GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
+ GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
+ GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
+ GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
+
+ public static Texture loadFromStreams(ClassLoader scope,
+ String basename,
+ String suffix,
+ boolean mipmapped) throws IOException, GLException {
+ Texture cubemap = TextureIO.newTexture(GL.GL_TEXTURE_CUBE_MAP);
+
+ for (int i = 0; i < suffixes.length; i++) {
+ String resourceName = basename + suffixes[i] + "." + suffix;
+ TextureData data = TextureIO.newTextureData(scope.getResourceAsStream(resourceName),
+ mipmapped,
+ TextureIO.getFileSuffix(resourceName));
+ if (data == null) {
+ throw new IOException("Unable to load texture " + resourceName);
+ }
+ cubemap.updateImage(data, targets[i]);
+ }
+
+ return cubemap;
+ }
+}
diff --git a/src/demos/vertexProgRefract/VertexProgRefract.java b/src/demos/vertexProgRefract/VertexProgRefract.java
index a01a5d7..00e1d4c 100644
--- a/src/demos/vertexProgRefract/VertexProgRefract.java
+++ b/src/demos/vertexProgRefract/VertexProgRefract.java
@@ -46,7 +46,6 @@ import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.utils.*;
-import com.sun.opengl.utils.*;
import demos.common.*;
import demos.util.*;
import gleem.*;
@@ -100,7 +99,7 @@ public class VertexProgRefract extends Demo {
private boolean firstRender = true;
private int vtxProg;
private int fragProg;
- private int cubemap;
+ private Texture cubemap;
private int bunnydl;
private int obj;
@@ -269,16 +268,11 @@ public class VertexProgRefract extends Demo {
gl.glProgramEnvParameter4fARB(GL.GL_VERTEX_PROGRAM_ARB, 2, 1.0f, -1.0f, 1.0f, 0.0f); // texture scale
gl.glProgramEnvParameter4fARB(GL.GL_VERTEX_PROGRAM_ARB, 3, 0.0f, 1.0f, 2.0f, 3.0f); // misc constants
- int[] cubemapTmp = new int[1];
- gl.glGenTextures(1, cubemapTmp, 0);
- cubemap = cubemapTmp[0];
- gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, cubemap);
-
- gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
- gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
-
try {
- loadPNGCubemap(gl, "demos/data/cubemaps/uffizi", true);
+ cubemap = Cubemap.loadFromStreams(getClass().getClassLoader(),
+ "demos/data/cubemaps/uffizi_",
+ "png",
+ true);
} catch (IOException e) {
shutdownDemo();
throw new RuntimeException(e);
@@ -381,16 +375,16 @@ public class VertexProgRefract extends Demo {
// set texture transforms
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, cubemap);
- gl.glEnable(GL.GL_TEXTURE_CUBE_MAP);
+ cubemap.bind();
+ cubemap.enable();
gl.glMatrixMode(GL.GL_TEXTURE);
gl.glLoadIdentity();
gl.glScalef(1.0f, -1.0f, 1.0f);
viewer.updateInverseRotation(gl);
gl.glActiveTexture(GL.GL_TEXTURE1);
- gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, cubemap);
- gl.glEnable(GL.GL_TEXTURE_CUBE_MAP);
+ cubemap.bind();
+ cubemap.enable();
gl.glMatrixMode(GL.GL_TEXTURE);
gl.glLoadIdentity();
gl.glScalef(1.0f, -1.0f, 1.0f);
@@ -454,6 +448,7 @@ public class VertexProgRefract extends Demo {
//
public void shutdownDemo() {
if (drawable != null) {
+ viewer.detach();
ManipManager.getManipManager().unregisterWindow(drawable);
drawable.removeGLEventListener(this);
}
@@ -508,60 +503,6 @@ public class VertexProgRefract extends Demo {
return b[((int) key) & 0xFF];
}
- // FIXME: note we found that we had to swap the negy and posy portions of the cubemap.
- // Not sure why this is the case. Vertical flip in the image read? Possible, but doesn't
- // appear to be the case (have tried this and produced wrong results at the time).
- String[] suffixes = { "posx", "negx", "negy", "posy", "posz", "negz" };
- int[] targets = { GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X,
- GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
- GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
- GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
- GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
- GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };
- private void loadPNGCubemap(GL gl, String baseName, boolean mipmapped) throws IOException {
- for (int i = 0; i < suffixes.length; i++) {
- String resourceName = baseName + "_" + suffixes[i] + ".png";
- // Note: use of BufferedInputStream works around 4764639/4892246
- BufferedImage img = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(resourceName)));
- if (img == null) {
- throw new RuntimeException("Error reading PNG image " + resourceName);
- }
- makeRGBTexture(gl, img, targets[i], mipmapped);
- }
- }
-
- private void makeRGBTexture(GL gl, BufferedImage img, int target, boolean mipmapped) {
- switch (img.getType()) {
- case BufferedImage.TYPE_3BYTE_BGR:
- case BufferedImage.TYPE_CUSTOM: {
- byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
- if (mipmapped) {
- glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB,
- GL.GL_UNSIGNED_BYTE, ByteBuffer.wrap(data));
- } else {
- gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0,
- GL.GL_RGB, GL.GL_UNSIGNED_BYTE, ByteBuffer.wrap(data));
- }
- break;
- }
-
- case BufferedImage.TYPE_INT_RGB: {
- int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
- if (mipmapped) {
- glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB,
- GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(data));
- } else {
- gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0,
- GL.GL_RGB, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(data));
- }
- break;
- }
-
- default:
- throw new RuntimeException("Unsupported image type " + img.getType());
- }
- }
-
private void initExtension(GL gl, String glExtensionName) {
if (!gl.isExtensionAvailable(glExtensionName)) {
final String message = "OpenGL extension \"" + glExtensionName + "\" not available";
@@ -650,8 +591,8 @@ public class VertexProgRefract extends Demo {
gl.glDisable(GL.GL_TEXTURE_CUBE_MAP);
gl.glActiveTexture(GL.GL_TEXTURE0);
- gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, cubemap);
- gl.glEnable(GL.GL_TEXTURE_CUBE_MAP);
+ cubemap.bind();
+ cubemap.enable();
// This is a workaround for a driver bug on Mac OS X where the
// normals are not being sent down to the hardware in