(Me again) Texture won't render. Loaded from a BMP loader that I've verified

Started by majikchicken, April 30, 2016, 20:36:32

Previous topic - Next topic

majikchicken

I'm trying to simply draw a texture across quad.  But it just draws a white quad (?).  I know the bitmap loader I wrote works correctly because I can use it to reproduce the texture.bmp file.  I've checked that the ByteBuffer in glTexImage2D has the correct values, but it still doesn't render the texture.

Here's my texture loading code:
...
		dataPos = header[0x0A] + header[0x0B] + header[0x0C] + header[0x0D];
		CLI.getDebugger().print("dataPos = " + dataPos + " & " + String.format("0x%02X", header[0x0A]));
		imageSize = header[0x22] * 1000 + header[0x23] * 100 + header[0x24] * 10 + header[0x25] * 1;
		String imgSizeStr = "0x" + imageSize;
		CLI.getDebugger().print("imgSizeStr = " + imgSizeStr + " + is = " + imageSize);
		imageSize = Integer.decode(imgSizeStr);
		CLI.getDebugger().print("imageSize = " + imageSize + " <> " + (wholeFile.length - dataPos));
		width = header[0x12] * 1 + header[0x13] * 10 + header[0x14] * 100 + header[0x15] * 1000; // Why the hell is this part of the header big endien but the others are little endien? The <redacted>?
		CLI.getDebugger().print("width = " + width);
		height = header[0x16] * 1 + header[0x17] * 10 + header[0x18] * 100 + header[0x19] * 1000; // Same here with the endien stuff
		CLI.getDebugger().print("height = " + height);
		
		// In case the header was missing info
		if(imageSize == 0) // It's an RGB bitmap, so it only has a mix of RGB values to make a color
			imageSize = width * height * 3; 
		if(dataPos == 0) // Ugh
			dataPos = 54; // End of BMP header
		
		dataBytes = new byte[imageSize];
		
		for(int i = 0; i < imageSize; i++)
			dataBytes[i] = wholeFile[i + dataPos];
			
		pic.data = dataBytes;
		pic.dbuf = ByteBuffer.wrap(pic.data);
		pic.width = width;
		pic.height = height;
		
		pic.dbuf.flip();
		
		//glGetError();
		
		pic.id = glGenTextures();
		
		glEnable(GL11.GL_TEXTURE_2D);
		glBindTexture(GL11.GL_TEXTURE_2D, pic.id);
		
		glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
		//CLI.throwError("gle0 = " + glGetError());
		glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
		//CLI.throwError("gle2 = " + glGetError());
		glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
		//CLI.throwError("gle3 = " + glGetError());
		glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
		glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
		glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGR, width, height, 0, GL12.GL_BGR, GL11.GL_UNSIGNED_BYTE, pic.dbuf);
		//CLI.throwError("gle1 = " + glGetError());
		
		glGenerateMipMap(GL11.GL_TEXTURE_2D);
		
		glBindTexture(GL11.GL_TEXTURE_2D, 0); // Unbind texture
		CLI.throwError("gle4 = " + glGetError());
		glDisable(GL11.GL_TEXTURE_2D);
		
		CLI.throwError("gle = " + glGetError());
...


Here's the render function using fixed-pipeline functions
...
			glBindTexture(GL11.GL_TEXTURE_2D, Textures.unknownTexture.id);
			glMatrixMode(GL11.GL_PROJECTION);
			glLoadIdentity();
			glOrtho(-halfWindowWidth, halfWindowWidth, -halfWindowHeight, halfWindowHeight, 0, Screen.windowDepth);
			glMatrixMode(GL11.GL_MODELVIEW);
			glLoadIdentity();
			
			...

			glEnable(GL11.GL_TEXTURE_2D);
			
			glColor3f(1.0f, 1.0f, 1.0f);
			glBegin(GL11.GL_QUADS);
			{
				glTexCoord2f(1.0f, 1.0f);
				glVertex2f(150, 150);
				
				glTexCoord2f(0.0f, 1.0f);
				glVertex2f(-150, 150);
				
				glTexCoord2f(0.0f, 0.0f);
				glVertex2f(-150, -150);
				
				glTexCoord2f(1.0f, 0.0f);
				glVertex2f(150, -150);
			}
			
			glEnd();
			
			glDisable(GL11.GL_TEXTURE_2D);
...


AFAIK, I'm doing this correctly, but I know it's my code that's doing it lol.  How can I fix this?

majikchicken

I solved it.  It was (once again) an issue because ByteBuffer.wrap() doesn't return a natively ordered buffer.  I had to change

pic.data = dataBytes;
pic.dbuf = ByteBuffer.wrap(pic.data);
pic.width = width;
pic.height = height;
         
pic.dbuf.flip();


to:

pic.data = dataBytes;
pic.dbuf = BufferUtils.createByteBuffer(imageSize);
pic.dbuf.put(pic.data);
pic.width = width;
pic.height = height;
		
pic.dbuf.rewind();