diff options
author | Kenneth Russel <kbrussel@alum.mit.edu> | 2005-05-27 19:45:51 +0000 |
---|---|---|
committer | Kenneth Russel <kbrussel@alum.mit.edu> | 2005-05-27 19:45:51 +0000 |
commit | 9d209d3b6ae12604e666d7b655bd1f19e70ee48b (patch) | |
tree | 5eb780835a316cbb62a9718a2757a3d51c31adb5 /src/demos/hdr/HDRTexture.java | |
parent | 02acfb8e24959f43cada34366598236a51b782bb (diff) |
Added Java/JOGL port of NVidia HDR demo.
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/jogl-demos/trunk@80 3298f667-5e0e-4b4a-8ed4-a3559d26a5f4
Diffstat (limited to 'src/demos/hdr/HDRTexture.java')
-rwxr-xr-x | src/demos/hdr/HDRTexture.java | 494 |
1 files changed, 494 insertions, 0 deletions
diff --git a/src/demos/hdr/HDRTexture.java b/src/demos/hdr/HDRTexture.java new file mode 100755 index 0000000..fcaac11 --- /dev/null +++ b/src/demos/hdr/HDRTexture.java @@ -0,0 +1,494 @@ +package demos.hdr; + +import java.io.*; + +import net.java.games.jogl.*; + +public class HDRTexture { + private RGBE.Header header; + private byte[] m_data; + private float[] m_floatdata; + private int m_width, m_height; + private float m_max_r, m_max_g, m_max_b; + private float m_min_r, m_min_g, m_min_b; + private float m_max; + private int m_target; + + public HDRTexture(String filename) throws IOException { + this(new FileInputStream(filename)); + } + + public HDRTexture(InputStream in) throws IOException { + DataInputStream datain = new DataInputStream(new BufferedInputStream(in)); + header = RGBE.readHeader(datain); + m_width = header.getWidth(); + m_height = header.getHeight(); + m_data = new byte[m_width * m_height * 4]; + RGBE.readPixelsRawRLE(datain, m_data, 0, m_width, m_height); + System.err.println("Loaded HDR image " + m_width + " x " + m_height); + } + + public byte[] getData() { return m_data; } + public int getPixelIndex(int x, int y) { + return ((m_width * (m_height - 1 - y)) + x) * 4; + } + public float[] getFloatData() { return m_floatdata; } + public int getPixelFloatIndex(int x, int y) { + return ((m_width * (m_height - 1 - y)) + x) * 3; + } + + public void analyze() { + m_max_r = m_max_g = m_max_b = 0.0f; + m_min_r = m_min_g = m_min_b = 1e10f; + int mine = 255; + int maxe = 0; + + int ptr = 0; + float[] rgb = new float[3]; + for(int i=0; i<m_width*m_height; i++) { + int e = m_data[ptr+3] & 0xFF; + if (e < mine) mine = e; + if (e > maxe) maxe = e; + + RGBE.rgbe2float(rgb, m_data, ptr); + float r = rgb[0]; + float g = rgb[1]; + float b = rgb[2]; + if (r > m_max_r) m_max_r = r; + if (g > m_max_g) m_max_g = g; + if (b > m_max_b) m_max_b = b; + if (r < m_min_r) m_min_r = r; + if (g < m_min_g) m_min_g = g; + if (b < m_min_b) m_min_b = b; + ptr += 4; + } + System.err.println("max intensity: " + m_max_r + " " + m_max_g + " " + m_max_b); + System.err.println("min intensity: " + m_min_r + " " + m_min_g + " " + m_min_b); + System.err.println("max e: " + maxe + " = " + RGBE.ldexp(1.0, maxe-128)); + System.err.println("min e: " + mine + " = " + RGBE.ldexp(1.0, mine-128)); + + m_max = m_max_r; + if (m_max_g > m_max) m_max = m_max_g; + if (m_max_b > m_max) m_max = m_max_b; + System.err.println("max: " + m_max); + } + + /** Converts from RGBE to floating-point RGB data. */ + public void convert() { + m_floatdata = new float [m_width*m_height*3]; + + int src = 0; + int dest = 0; + float[] rgb = new float[3]; + for(int i=0; i<m_width*m_height; i++) { + RGBE.rgbe2float(rgb, m_data, src); + + m_floatdata[dest++] = remap(rgb[0], m_max); + m_floatdata[dest++] = remap(rgb[1], m_max); + m_floatdata[dest++] = remap(rgb[2], m_max); + + src += 4; + } + } + + public int create2DTextureRGBE(GL gl, int targetTextureType) { + m_target = targetTextureType; + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp); + int texid = tmp[1]; + + gl.glBindTexture(m_target, texid); + + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); + gl.glTexParameteri(m_target, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE); + gl.glTexImage2D(m_target, 0, GL.GL_RGBA, m_width, m_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, m_data); + + return texid; + } + + public int create2DTextureHILO(GL gl, int targetTextureType, boolean rg) { + m_target = targetTextureType; + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp); + int texid = tmp[0]; + + gl.glBindTexture(m_target, texid); + + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); + gl.glTexParameteri(m_target, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE); + + float[] img = new float [m_width * m_height * 2]; + int src = 0; + int dest = 0; + for (int j=0; j<m_height; j++) { + for (int i=0; i<m_width; i++) { + if (rg) { + img[dest++] = m_floatdata[src + 0]; + img[dest++] = m_floatdata[src + 1]; + } else { + img[dest++] = m_floatdata[src + 2]; + img[dest++] = 0; + } + src+=3; + } + } + + gl.glTexImage2D(m_target, 0, GL.GL_HILO16_NV, m_width, m_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, img); + + return texid; + } + + // create a cubemap texture from a 2D image in cross format (thanks to Jonathon McGee) + public int createCubemapRGBE(GL gl) { + // cross is 3 faces wide, 4 faces high + int face_width = m_width / 3; + int face_height = m_height / 4; + byte[] face = new byte[face_width * face_height * 4]; + + m_target = GL.GL_TEXTURE_CUBE_MAP_ARB; + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp); + int texid = tmp[0]; + gl.glBindTexture(m_target, texid); + + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); + gl.glTexParameteri(m_target, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE); + + // gl.glTexParameteri(m_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + // gl.glTexParameteri(m_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + + // extract 6 faces + + // positive Y + int ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelIndex(2 * face_width - (i + 1), 3 * face_height + j); + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, GL.GL_RGBA, face_width, face_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, face); + + // positive X + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelIndex(i, m_height - (face_height + j + 1)); + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL.GL_RGBA, face_width, face_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, face); + + // negative Z + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelIndex(face_width + i, m_height - (face_height + j + 1)); + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, GL.GL_RGBA, face_width, face_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, face); + + // negative X + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelIndex(2 * face_width + i, m_height - (face_height + j + 1)); + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, GL.GL_RGBA, face_width, face_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, face); + + // negative Y + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelIndex(2 * face_width - (i + 1), face_height + j); + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, GL.GL_RGBA, face_width, face_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, face); + + // positive Z + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelIndex(2 * face_width - (i + 1), j); + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + face[ptr++] = m_data[src++]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, GL.GL_RGBA, face_width, face_height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, face); + + return texid; + } + + public int createCubemapHILO(GL gl, boolean rg) { + // cross is 3 faces wide, 4 faces high + int face_width = m_width / 3; + int face_height = m_height / 4; + float[] face = new float [face_width * face_height * 2]; + + m_target = GL.GL_TEXTURE_CUBE_MAP_ARB; + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp); + int texid = tmp[0]; + gl.glBindTexture(m_target, texid); + + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); + gl.glTexParameteri(m_target, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE); + + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + + // extract 6 faces + + // positive Y + int ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width - (i + 1), 3 * face_height + j); + if (rg) { + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + } else { + face[ptr++] = m_floatdata[src + 2]; + face[ptr++] = 0; + } + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, GL.GL_HILO16_NV, face_width, face_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, face); + + // positive X + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(i, m_height - (face_height + j + 1)); + if (rg) { + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + } else { + face[ptr++] = m_floatdata[src + 2]; + face[ptr++] = 0; + } + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL.GL_HILO16_NV, face_width, face_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, face); + + // negative Z + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(face_width + i, m_height - (face_height + j + 1)); + if (rg) { + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + } else { + face[ptr++] = m_floatdata[src + 2]; + face[ptr++] = 0; + } + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, GL.GL_HILO16_NV, face_width, face_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, face); + + // negative X + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width + i, m_height - (face_height + j + 1)); + if (rg) { + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + } else { + face[ptr++] = m_floatdata[src + 2]; + face[ptr++] = 0; + } + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, GL.GL_HILO16_NV, face_width, face_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, face); + + // negative Y + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width - (i + 1), face_height + j); + if (rg) { + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + } else { + face[ptr++] = m_floatdata[src + 2]; + face[ptr++] = 0; + } + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, GL.GL_HILO16_NV, face_width, face_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, face); + + // positive Z + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width - (i + 1), j); + if (rg) { + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + } else { + face[ptr++] = m_floatdata[src + 2]; + face[ptr++] = 0; + } + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, GL.GL_HILO16_NV, face_width, face_height, 0, GL.GL_HILO_NV, GL.GL_FLOAT, face); + + return texid; + } + + public int createCubemap(GL gl, int format) { + // cross is 3 faces wide, 4 faces high + int face_width = m_width / 3; + int face_height = m_height / 4; + float[] face = new float [face_width * face_height * 3]; + + m_target = GL.GL_TEXTURE_CUBE_MAP_ARB; + int[] tmp = new int[1]; + gl.glGenTextures(1, tmp); + int texid = tmp[0]; + gl.glBindTexture(m_target, texid); + + gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); + gl.glTexParameteri(m_target, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE); + + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + gl.glTexParameteri(m_target, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + + // extract 6 faces + + // positive Y + int ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width - (i + 1), 3 * face_height + j); + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + face[ptr++] = m_floatdata[src + 2]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, format, face_width, face_height, 0, GL.GL_RGB, GL.GL_FLOAT, face); + + // positive X + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(i, m_height - (face_height + j + 1)); + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + face[ptr++] = m_floatdata[src + 2]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, format, face_width, face_height, 0, GL.GL_RGB, GL.GL_FLOAT, face); + + // negative Z + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(face_width + i, m_height - (face_height + j + 1)); + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + face[ptr++] = m_floatdata[src + 2]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, format, face_width, face_height, 0, GL.GL_RGB, GL.GL_FLOAT, face); + + // negative X + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width + i, m_height - (face_height + j + 1)); + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + face[ptr++] = m_floatdata[src + 2]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, format, face_width, face_height, 0, GL.GL_RGB, GL.GL_FLOAT, face); + + // negative Y + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width - (i + 1), face_height + j); + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + face[ptr++] = m_floatdata[src + 2]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, format, face_width, face_height, 0, GL.GL_RGB, GL.GL_FLOAT, face); + + // positive Z + ptr = 0; + for (int j=0; j<face_height; j++) { + for (int i=0; i<face_width; i++) { + int src = getPixelFloatIndex(2 * face_width - (i + 1), j); + face[ptr++] = m_floatdata[src + 0]; + face[ptr++] = m_floatdata[src + 1]; + face[ptr++] = m_floatdata[src + 2]; + } + } + gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, format, face_width, face_height, 0, GL.GL_RGB, GL.GL_FLOAT, face); + + return texid; + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private static float remap(float x, float max) { + if (x > max) x = max; + return (float) Math.sqrt(x / max); + } + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + try { + HDRTexture tex = new HDRTexture(args[i]); + tex.analyze(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} |