Generating Mipmap Problems

I’m doing my on MIPmap generation and I’ve gotten content to display on the screen at lower resolutions it doesn’t appear quite right:

http://captiveimagination.com/download/test_mipmap01.jpg

If I expand the window to be larger so it doesn’t use the mipmapping then I get the correctly displaying image:

http://captiveimagination.com/download/test_mipmap02.jpg

The following is the code I’m using and though I know it’s incredibly inefficient I am simply trying to learn how to generate mipmaps on my own:

package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;

import com.sun.opengl.util.BufferUtil;

public class TestMipmapping extends TestBasic {
	private int[] vertexes;
	private int[] textures;
	private int[] textureVbo;
	
	private int programId;
	private int vertexShaderId;
	private int fragmentShaderId;
	
	private FloatBuffer textureCoordinates;
	
	public void initialize(GL gl) {
		super.initialize(gl);
		
		BufferedImage image = null;
		
		try {
			image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
		} catch(Throwable t) {
			t.printStackTrace();
		}
		
		// Allocate my buffer to hold my two triangles to make up a quad
		FloatBuffer buffer = BufferUtil.newFloatBuffer(12); {
			buffer.put(0.0f);		// Bottom-Left
			buffer.put(0.0f);
			buffer.put(0.0f);
			
			buffer.put(image.getWidth());		// Bottom-Right
			buffer.put(0.0f);
			buffer.put(0.0f);
			
			buffer.put(image.getWidth());		// Top-Right
			buffer.put(image.getHeight());
			buffer.put(0.0f);
			
			buffer.put(0.0f);		// Top-Left
			buffer.put(image.getHeight());
			buffer.put(0.0f);
			
			buffer.rewind();
		}
		
		vertexes = new int[1];
		gl.glGenBuffersARB(1, vertexes, 0);																		// Generate a buffer id
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);												// Bind the buffer
		gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, buffer.capacity() * 4, buffer, GL.GL_STATIC_DRAW_ARB);		// Send the vertex buffer data to the video card
		
		textureCoordinates = BufferUtil.newFloatBuffer(8); {
			textureCoordinates.put(0.0f);		// Top-Left
			textureCoordinates.put(1.0f);
			
			textureCoordinates.put(1.0f);		// Top-Right
			textureCoordinates.put(1.0f);
			
			textureCoordinates.put(1.0f);		// Bottom-Right
			textureCoordinates.put(0.0f);
			
			textureCoordinates.put(0.0f);		// Bottom-Left
			textureCoordinates.put(0.0f);
			
			textureCoordinates.rewind();
		}
		
		textureVbo = new int[1];
		gl.glGenBuffersARB(1, textureVbo, 0);
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
		gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, textureCoordinates.capacity() * 4, textureCoordinates, GL.GL_STATIC_DRAW_ARB);
		
		// Create and bind texture
		textures = new int[1];
		gl.glGenTextures(1, textures, 0);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
		try {
			DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
			byte[] data = dbb.getData();
			ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
			pixels.put(data);
			pixels.flip();
			
			int mode = 3;			// Modes: 1 = no mipmapping, 2 = auto-mipmapping, 3 = custom mipmapping
			
			gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
			gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, mode == 1 ? GL.GL_LINEAR : GL.GL_LINEAR_MIPMAP_LINEAR);
			if (mode == 2) {
				gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
			}
			gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, image.getWidth(), image.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
			if (mode == 3) {
				// Generate mipmaps
				int w = image.getWidth();
				int h = image.getHeight();
				int position = 1;
				while ((w > 1) || (h > 1)) {
					w /= 2;
					h /= 2;
					
					if (w == 0) {
						w = 1;
					} else if (h == 0) {
						h = 1;
					}
					
					BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
					Graphics2D g = bi.createGraphics();
					g.drawImage(image, 0, 0, w, h, null);
					g.dispose();
					
					DataBufferByte dbb2 = (DataBufferByte)bi.getRaster().getDataBuffer();
					byte[] data2 = dbb2.getData();
					ByteBuffer pixels2 = BufferUtil.newByteBuffer(data2.length);
					pixels2.put(data2);
					pixels2.flip();
					
					gl.glTexImage2D(GL.GL_TEXTURE_2D, position, GL.GL_RGB, bi.getWidth(), bi.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels2);
					
					position++;
				}
				
				System.out.println("Positions Created: " + position);
			}
		} catch(Throwable t) {
			t.printStackTrace();
			
		}
		
		// Load the GLSL texturing functionality
		programId = gl.glCreateProgramObjectARB();
		
		// Vertex Shader
		vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
		String source = getShaderSource("texture_coordinates.vert");
		gl.glShaderSourceARB(vertexShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
		gl.glCompileShaderARB(vertexShaderId);
		int[] result = new int[1];
		gl.glGetShaderiv(vertexShaderId, GL.GL_COMPILE_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Compile of GLSL Shader failed!");
		}
		gl.glAttachObjectARB(programId, vertexShaderId);
		
		// Fragment Shader
		fragmentShaderId = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
		source = getShaderSource("texture_coordinates.frag");
		gl.glShaderSourceARB(fragmentShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
		gl.glCompileShaderARB(fragmentShaderId);
		result = new int[1];
		gl.glGetShaderiv(fragmentShaderId, GL.GL_COMPILE_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Compile of GLSL Shader failed!");
		}
		gl.glAttachObjectARB(programId, fragmentShaderId);
		
		// Link program
		gl.glLinkProgramARB(programId);
		gl.glGetProgramiv(programId, GL.GL_LINK_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Linking of GLSL Shader failed!");
		}
		
		// Validate log results
		gl.glValidateProgramARB(programId);
		gl.glGetObjectParameterivARB(programId, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, result, 0);
		int length = result[0];
		if (length > 0) {
			byte[] log = new byte[length];
			gl.glGetInfoLogARB(programId, log.length, new int[length], 0, log, 0);
			System.out.println("Log Result: " + new String(log) + " (" + length + ")");
		}
		
		gl.glUseProgramObjectARB(programId);
		
		int myTexture = gl.glGetUniformLocationARB(programId, "myTexture");
		gl.glUniform1iARB(myTexture, 0);
	}
	
	public void draw(GL gl) {
		gl.glTranslatef(-400.0f, 0.0f, -800.0f);
		
		gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
		gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

		gl.glEnableClientState(GL.GL_VERTEX_ARRAY); {
			gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);
			gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);		
			gl.glDrawArrays(GL.GL_QUADS, 0, 4);
		}
		gl.glDisableClientState(GL.GL_VERTEX_ARRAY);	
	}
	
	public static void main(String[] args) throws Exception {
		new TestMipmapping();
	}
}

Any help is greatly appreciated.

My guess is that you’re looping through the layers incorrectly so that not all image levels are configured, and the weird image is referencing bad texture data.

You can calculate the total number of mipmaps in a 2d texture using:


(int) Math.floor(Math.log(Math.max(width, height)) / Math.log(2)) + 1;

Then change your loop to:


for (int i = 1; i < numMipmaps; i++) {
   w = Math.max(1, w / 2);
   h = Math.max(1, h / 2);

   ... then proceed as before, except use i in glTexImage2D instead of position
}

Make sure to start the loop at 1 since you make the 0th level image outside of it in your code.

Let me know if that changes anything.

This one is quite obvious: in your 2nd mipmap level, your pixels are off by -1 pixel per row.

Probably caused by:


					w /= 2;
					h /= 2;

You might want to try to round up.

Rounding up seemed to cause me to end up with one too few mipmap entries.

This approach left me with the exact same results.

You don’t need to specify the maximum number of mipmap levels.

Ofcourse.

Well, I guess more relevantly, nothing displays if I do it that way.

I don’t know what the actual dimensions are for your images, but try adding gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1) before you start calling glTexImage2D.

Hey thanks, that seems to have fixed that problem, but caused a new one. :slight_smile:

http://captiveimagination.com/download/test_mipmap03.jpg

As I make the window smaller it becomes more blue, and as I make it larger it gets closer to the correct yellow color my duck should be. :o I output the generated images and they all appear yellow as they should.

Hm…

correct => Yellow = (255,255,0), Red = (255,0,0)
incorrect => Aqua = (0,255,255), Blue = (0,0,255)

So the Red and Blue channel are swapped.

That makes sense, but I’m at a loss as to how to fix it. I tried switching to GL_BGR but that stops anything from appearing. I’m matching my mipmapping exactly to my original 0 call so I don’t understand why the mipmaps would be incorrect?

Here is the current code:

package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;

import com.sun.opengl.util.BufferUtil;

public class TestMipmapping extends TestBasic {
	private int[] vertexes;
	private int[] textures;
	private int[] textureVbo;
	
	private int programId;
	private int vertexShaderId;
	private int fragmentShaderId;
	
	private FloatBuffer textureCoordinates;
	
	public void initialize(GL gl) {
		super.initialize(gl);
		
		BufferedImage image = null;
		
		try {
			image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
		} catch(Throwable t) {
			t.printStackTrace();
		}
		
		// Allocate my buffer to hold my two triangles to make up a quad
		FloatBuffer buffer = BufferUtil.newFloatBuffer(12); {
			buffer.put(0.0f);		// Bottom-Left
			buffer.put(0.0f);
			buffer.put(0.0f);
			
			buffer.put(image.getWidth());		// Bottom-Right
			buffer.put(0.0f);
			buffer.put(0.0f);
			
			buffer.put(image.getWidth());		// Top-Right
			buffer.put(image.getHeight());
			buffer.put(0.0f);
			
			buffer.put(0.0f);		// Top-Left
			buffer.put(image.getHeight());
			buffer.put(0.0f);
			
			buffer.rewind();
		}
		
		vertexes = new int[1];
		gl.glGenBuffersARB(1, vertexes, 0);																		// Generate a buffer id
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);												// Bind the buffer
		gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, buffer.capacity() * 4, buffer, GL.GL_STATIC_DRAW_ARB);		// Send the vertex buffer data to the video card
		
		textureCoordinates = BufferUtil.newFloatBuffer(8); {
			textureCoordinates.put(0.0f);		// Top-Left
			textureCoordinates.put(1.0f);
			
			textureCoordinates.put(1.0f);		// Top-Right
			textureCoordinates.put(1.0f);
			
			textureCoordinates.put(1.0f);		// Bottom-Right
			textureCoordinates.put(0.0f);
			
			textureCoordinates.put(0.0f);		// Bottom-Left
			textureCoordinates.put(0.0f);
			
			textureCoordinates.rewind();
		}
		
		textureVbo = new int[1];
		gl.glGenBuffersARB(1, textureVbo, 0);
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
		gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, textureCoordinates.capacity() * 4, textureCoordinates, GL.GL_STATIC_DRAW_ARB);
		
		// Create and bind texture
		textures = new int[1];
		gl.glGenTextures(1, textures, 0);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
		try {
			DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
			byte[] data = dbb.getData();
			ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
			pixels.put(data);
			pixels.flip();
			
			int mode = 3;			// Modes: 1 = no mipmapping, 2 = auto-mipmapping, 3 = custom mipmapping
			
			gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
			gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, mode == 1 ? GL.GL_LINEAR : GL.GL_LINEAR_MIPMAP_LINEAR);
			if (mode == 2) {
				gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
			}
			gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
			gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, image.getWidth(), image.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
			if (mode == 3) {
				// Generate mipmaps
				int mipmaps = (int)Math.floor(Math.log(Math.max(image.getWidth(), image.getHeight())) / Math.log(2)) + 1;
				int w = image.getWidth();
				int h = image.getHeight();
				for (int i = 1; i < mipmaps; i++) {
					w = Math.max(1, w / 2);
					h = Math.max(1, h / 2);
					
					if (w == 0) {
						w = 1;
					} else if (h == 0) {
						h = 1;
					}
					
					System.out.println(w + "x" + h + " - " + i);
					
					BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
					Graphics2D g = bi.createGraphics();
					g.drawImage(image, 0, 0, w, h, null);
					g.dispose();
					
					DataBufferByte dbb2 = (DataBufferByte)bi.getRaster().getDataBuffer();
					byte[] data2 = dbb2.getData();
					ByteBuffer pixels2 = BufferUtil.newByteBuffer(data2.length);
					pixels2.put(data2);
					pixels2.flip();
					
					gl.glTexImage2D(GL.GL_TEXTURE_2D, i, GL.GL_RGB, bi.getWidth(), bi.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels2);
				}
				
				System.out.println("Positions Created: " + mipmaps);
			}
		} catch(Throwable t) {
			t.printStackTrace();
			
		}
		
		// Load the GLSL texturing functionality
		programId = gl.glCreateProgramObjectARB();
		
		// Vertex Shader
		vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
		String source = getShaderSource("texture_coordinates.vert");
		gl.glShaderSourceARB(vertexShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
		gl.glCompileShaderARB(vertexShaderId);
		int[] result = new int[1];
		gl.glGetShaderiv(vertexShaderId, GL.GL_COMPILE_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Compile of GLSL Shader failed!");
		}
		gl.glAttachObjectARB(programId, vertexShaderId);
		
		// Fragment Shader
		fragmentShaderId = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
		source = getShaderSource("texture_coordinates.frag");
		gl.glShaderSourceARB(fragmentShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
		gl.glCompileShaderARB(fragmentShaderId);
		result = new int[1];
		gl.glGetShaderiv(fragmentShaderId, GL.GL_COMPILE_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Compile of GLSL Shader failed!");
		}
		gl.glAttachObjectARB(programId, fragmentShaderId);
		
		// Link program
		gl.glLinkProgramARB(programId);
		gl.glGetProgramiv(programId, GL.GL_LINK_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Linking of GLSL Shader failed!");
		}
		
		// Validate log results
		gl.glValidateProgramARB(programId);
		gl.glGetObjectParameterivARB(programId, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, result, 0);
		int length = result[0];
		if (length > 0) {
			byte[] log = new byte[length];
			gl.glGetInfoLogARB(programId, log.length, new int[length], 0, log, 0);
			System.out.println("Log Result: " + new String(log) + " (" + length + ")");
		}
		
		gl.glUseProgramObjectARB(programId);
		
		int myTexture = gl.glGetUniformLocationARB(programId, "myTexture");
		gl.glUniform1iARB(myTexture, 0);
	}
	
	public void draw(GL gl) {
		gl.glTranslatef(-400.0f, 0.0f, -800.0f);
		
		gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
		gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

		gl.glEnableClientState(GL.GL_VERTEX_ARRAY); {
			gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);
			gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);		
			gl.glDrawArrays(GL.GL_QUADS, 0, 4);
		}
		gl.glDisableClientState(GL.GL_VERTEX_ARRAY);	
	}
	
	public static void main(String[] args) throws Exception {
		new TestMipmapping();
	}
}

It just occured to me that having swapped Red and Blue channels, is exactly the same as a completely swapped pixel, as green is the same because it is in the middle of the 3 bytes.

maybe this will fix it…
glPixelStorei(GL_PACK_SWAP_BYTES, GL_TRUE);
or…
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);

http://pyopengl.sourceforge.net/documentation/manual/glPixelStore.3G.html

Tried each individually and both at the same time but made no difference.

(First post here ever:)

My personal guess, although I am very unexperienced so don’t hurt me if I’m wrong, is that this line is the problem:

BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);

Note the ‘BGR’ at the end, opposed to the more common ‘RGB’, in effect having the R(ed) and B(lue) channel swapped…

Doh! :o

In my defense… I didn’t see it.

:-X :persecutioncomplex:

I thought that as well, but why does it work fine when not using mipmapping?

If I don’t enable mipmapping at all (or set mode = 1 in that code) everything looks perfectly fine.

Because


BufferedImage image = null;
		
		try {
			image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
		} catch(Throwable t) {
			t.printStackTrace();
		}

returns a BufferedImage that is RGB…? Only your scaled versions are BGR.

Because:


if (mode == 3)
{
	// Generate mipmaps
	<snip>
					
		BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);

	<snip>
}

In other words: the typo is only called in ‘mode 3’, or ‘custom mipmap mode’. In mode 1 or 2, it isn’t called at all, and thus the typo is never reached =)

[edit:] Clean-up and clarification. [/edit]

Hey, changing it fixed the problem. I swapped out for the alpha mode I know is supported and everything works correctly now.

Thanks guys. Just in case anyone is interested, here is the final source code:

package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;

import com.sun.opengl.util.BufferUtil;

public class TestMipmapping extends TestBasic {
	private int[] vertexes;
	private int[] textures;
	private int[] textureVbo;
	
	private int programId;
	private int vertexShaderId;
	private int fragmentShaderId;
	
	private FloatBuffer textureCoordinates;
	
	public void initialize(GL gl) {
		super.initialize(gl);
		
		BufferedImage image = null;
		
		try {
			image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
			BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
			Graphics2D g = tmp.createGraphics();
			g.drawImage(image, 0, 0, null);
			g.dispose();
			image = tmp;
		} catch(Throwable t) {
			t.printStackTrace();
		}
		
		// Allocate my buffer to hold my two triangles to make up a quad
		FloatBuffer buffer = BufferUtil.newFloatBuffer(12); {
			buffer.put(0.0f);		// Bottom-Left
			buffer.put(0.0f);
			buffer.put(0.0f);
			
			buffer.put(image.getWidth());		// Bottom-Right
			buffer.put(0.0f);
			buffer.put(0.0f);
			
			buffer.put(image.getWidth());		// Top-Right
			buffer.put(image.getHeight());
			buffer.put(0.0f);
			
			buffer.put(0.0f);		// Top-Left
			buffer.put(image.getHeight());
			buffer.put(0.0f);
			
			buffer.rewind();
		}
		
		vertexes = new int[1];
		gl.glGenBuffersARB(1, vertexes, 0);																		// Generate a buffer id
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);												// Bind the buffer
		gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, buffer.capacity() * 4, buffer, GL.GL_STATIC_DRAW_ARB);		// Send the vertex buffer data to the video card
		
		textureCoordinates = BufferUtil.newFloatBuffer(8); {
			textureCoordinates.put(0.0f);		// Top-Left
			textureCoordinates.put(1.0f);
			
			textureCoordinates.put(1.0f);		// Top-Right
			textureCoordinates.put(1.0f);
			
			textureCoordinates.put(1.0f);		// Bottom-Right
			textureCoordinates.put(0.0f);
			
			textureCoordinates.put(0.0f);		// Bottom-Left
			textureCoordinates.put(0.0f);
			
			textureCoordinates.rewind();
		}
		
		textureVbo = new int[1];
		gl.glGenBuffersARB(1, textureVbo, 0);
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
		gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, textureCoordinates.capacity() * 4, textureCoordinates, GL.GL_STATIC_DRAW_ARB);
		
		// Create and bind texture
		textures = new int[1];
		gl.glGenTextures(1, textures, 0);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
		try {
			DataBufferInt dbb = (DataBufferInt)image.getRaster().getDataBuffer();
			int[] data = dbb.getData();
			IntBuffer pixels = BufferUtil.newIntBuffer(data.length);
			pixels.put(data);
			pixels.flip();
			
			int mode = 3;			// Modes: 1 = no mipmapping, 2 = auto-mipmapping, 3 = custom mipmapping
			
			gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
			gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, mode == 1 ? GL.GL_LINEAR : GL.GL_LINEAR_MIPMAP_LINEAR);
			if (mode == 2) {
				gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
			}
			gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
			gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL.GL_BGRA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
			if (mode == 3) {
				// Generate mipmaps
				int mipmaps = (int)Math.floor(Math.log(Math.max(image.getWidth(), image.getHeight())) / Math.log(2)) + 1;
				int w = image.getWidth();
				int h = image.getHeight();
				for (int i = 1; i < mipmaps; i++) {
					w = Math.max(1, w / 2);
					h = Math.max(1, h / 2);
					
					if (w == 0) {
						w = 1;
					} else if (h == 0) {
						h = 1;
					}
					
					System.out.println(w + "x" + h + " - " + i);
					
					BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
					Graphics2D g = bi.createGraphics();
					g.drawImage(image, 0, 0, w, h, null);
					g.dispose();
					
					DataBufferInt dbb2 = (DataBufferInt)bi.getRaster().getDataBuffer();
					int[] data2 = dbb2.getData();
					IntBuffer pixels2 = BufferUtil.newIntBuffer(data2.length);
					pixels2.put(data2);
					pixels2.flip();
					
					gl.glTexImage2D(GL.GL_TEXTURE_2D, i, GL.GL_RGBA, bi.getWidth(), bi.getHeight(), 0, GL.GL_BGRA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, pixels2);
				}
				
				System.out.println("Positions Created: " + mipmaps);
			}
		} catch(Throwable t) {
			t.printStackTrace();
			
		}
		
		// Load the GLSL texturing functionality
		programId = gl.glCreateProgramObjectARB();
		
		// Vertex Shader
		vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
		String source = getShaderSource("texture_coordinates.vert");
		gl.glShaderSourceARB(vertexShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
		gl.glCompileShaderARB(vertexShaderId);
		int[] result = new int[1];
		gl.glGetShaderiv(vertexShaderId, GL.GL_COMPILE_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Compile of GLSL Shader failed!");
		}
		gl.glAttachObjectARB(programId, vertexShaderId);
		
		// Fragment Shader
		fragmentShaderId = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
		source = getShaderSource("texture_coordinates.frag");
		gl.glShaderSourceARB(fragmentShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
		gl.glCompileShaderARB(fragmentShaderId);
		result = new int[1];
		gl.glGetShaderiv(fragmentShaderId, GL.GL_COMPILE_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Compile of GLSL Shader failed!");
		}
		gl.glAttachObjectARB(programId, fragmentShaderId);
		
		// Link program
		gl.glLinkProgramARB(programId);
		gl.glGetProgramiv(programId, GL.GL_LINK_STATUS, result, 0);
		if (result[0] != GL.GL_TRUE) {
			throw new RuntimeException("Linking of GLSL Shader failed!");
		}
		
		// Validate log results
		gl.glValidateProgramARB(programId);
		gl.glGetObjectParameterivARB(programId, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, result, 0);
		int length = result[0];
		if (length > 0) {
			byte[] log = new byte[length];
			gl.glGetInfoLogARB(programId, log.length, new int[length], 0, log, 0);
			System.out.println("Log Result: " + new String(log) + " (" + length + ")");
		}
		
		gl.glUseProgramObjectARB(programId);
		
		int myTexture = gl.glGetUniformLocationARB(programId, "myTexture");
		gl.glUniform1iARB(myTexture, 0);
	}
	
	public void draw(GL gl) {
		gl.glTranslatef(-400.0f, 0.0f, -800.0f);
		
		gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
		gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
		gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

		gl.glEnableClientState(GL.GL_VERTEX_ARRAY); {
			gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);
			gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);		
			gl.glDrawArrays(GL.GL_QUADS, 0, 4);
		}
		gl.glDisableClientState(GL.GL_VERTEX_ARRAY);	
	}
	
	public static void main(String[] args) throws Exception {
		new TestMipmapping();
	}
}

					w = Math.max(1, w / 2);
					h = Math.max(1, h / 2);
					
					if (w == 0) {
						w = 1;
					} else if (h == 0) {
						h = 1;
					}

w and h will never be 0

Also, call flush in the BufferedImages you’re not going to use anymore. Otherwise you’ll run out of memory pretty quickly.

I’m going to revise this significantly now that I finally have something that works. :slight_smile:

This seems to appear on the screen exactly as it should…is there anything else I need to do in order to properly support NPOT textures?

Now the only hurdle I have left is PBOs.