Main Menu

[fixed]

Started by ScriptedAwesome, August 23, 2017, 18:05:19

Previous topic - Next topic

ScriptedAwesome

I'm getting this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 480000
   at Texture.<init>(Texture.java:32)
   at window.<init>(window.java:23)
   at window.main(window.java:58)
with this code:
import javax.imageio.ImageIO;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.BufferUtils;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Texture {
	
	private int id;
	private int width;
	private int height;
	
	public Texture(String filename) {
		BufferedImage bi;
		try {
			
			bi = ImageIO.read(new File(filename));
			width = bi.getWidth();
			height = bi.getHeight();
			
			int[] pixels_raw = new int[width * height];
			pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
			
			ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4);
			for(int j = 0; j < height; j++) { 
				for(int i = 0; i < width; i++) {
					int pixel = pixels_raw[i * width + j];
					pixels.put((byte) ((pixel >> 16) & 0xFF));//RED
					pixels.put((byte) ((pixel >> 8) & 0xFF));//GREEN
					pixels.put((byte) ((pixel >> 0) & 0xFF));//BLUE
					pixels.put((byte) ((pixel >> 24) & 0xFF));//ALPHA
				}
				
				
			}

			pixels.flip();
			id = glGenTextures();
			glBindTexture(GL_TEXTURE_2D, id);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
			
			
		}catch(IOException e) {
			
			e.printStackTrace();
			
		}
	}
	public void bind() {
		glBindTexture(GL_TEXTURE_2D, id);
	}
}

and also
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL;
public class window { {
	
		if(!glfwInit()) {
			
			System.err.println("GLFW Failed to initialize");
			System.exit(1);
		}
		long win = glfwCreateWindow(640, 480, "Quad!", 0, 0);
		

		
		glfwShowWindow(win);
		
		glfwMakeContextCurrent(win);
		
		GL.createCapabilities();
		
		glEnable(GL_TEXTURE_2D);
		
		Texture tex = new Texture("./res/java1.png");
		
		while(!glfwWindowShouldClose(win)) {
			if(glfwGetKey(win, GLFW_KEY_A) == GL_TRUE) {
				glfwSetWindowShouldClose(win, true);
			} 
			glfwPollEvents();
						
			tex.bind();
			
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-0.5f, 0.5f);
				
				glTexCoord2f(0, 1);
				glVertex2f(0.5f, 0.5f);
				
				glTexCoord2f(1, 1);
				glVertex2f(0.5f, -0.5f);
				
				glTexCoord2f(1, 0);
				glVertex2f(-0.5f, -0.5f);
				
			glEnd();
				
			glfwSwapBuffers(win);
			
		}
		
		
		
		glfwTerminate();
	}
	
	public static void main(String[] args) {
		new window();
	}
	
}


I am using Eclipse Neon, with a Windows 10, Java 8

darkyellow

Try changing this

pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);

To

bi.getRGB(0, 0, width, height, pixels_raw, 0, width);

Kai

The thread has already been renamed to "[fixed]" and the error was apparently in line 32 (at Texture.<init>(Texture.java:32)) which indeed was line 32 in the source listing:
int pixel = pixels_raw[i * width + j];

where 'i' runs over the width. Therefore, he was multiplying the 'i' (i.e. 'x' run variable) with the 'width'/stride which did not make much sense.
It should have been:
int pixel = pixels_raw[j * width + i];