OpenGL Array Texture Returns Only First or Last Texture

Started by CJ Burkey, April 04, 2018, 19:10:24

Previous topic - Next topic

CJ Burkey

(I'm going to end up filling this forum up with all my struggles)

So, basically, I've got an array texture being set up like this:

texture = glGenTextures();
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);	// 1 byte per component
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);	// Pixel perfect (with mipmapping)
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, w, h, images.length, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
try {
	for (int i = 0; i < images.length; i ++) {
		InputStream is = Texture.class.getResourceAsStream(images[i].getFullPath());
		if (is == null) {
			throw new FileNotFoundException("Could not locate texture file: " + images[i].getFullPath());
		}
		PNGDecoder img = new PNGDecoder(is);
		ByteBuffer buff = ByteBuffer.allocateDirect(4 * img.getWidth() * img.getHeight());
		img.decode(buff, img.getWidth() * 4, Format.RGBA);
		glPixelStorei(GL_UNPACK_ALIGNMENT, 4);	// 1 byte per component
		buff.flip();
		glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, buff);
		Debug.log("{} => {}", i, images[i].getFullPath());
	}
	
	glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
	
	isCreated = true;
} catch (Exception e) {
	Debug.error("Unable to create texture from path: {}", path);
	Debug.error(e, true);
}


And to access it, I pass the texture id to the shader as an integer like this:

bindVertexArray();
glBindBuffer(GL_ARRAY_BUFFER, tbo);
glBufferData(GL_ARRAY_BUFFER, tBuff, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_INT, false, 0, 0);
glDisableVertexAttribArray(2);
unbindVertexBuffer();
memFree(tBuff);
unbindVertexArray();


(tbo is the pointer to the buffer and tBuff is the integer buffer of ids)

Inside the shader, I simply do

fragColor = texture(samplerArr, vec3(uv, textureId));


However, only the last texture in the array is used unless the id is zero, in which case the first texture is used. I also tried sending through the total number of textures and diving those to return a vec3 with the z-value as a proportion rather than a specific id, but that only draw the first texture. With this information, can it be determined what I am doing wrong, or is there more to it?

(I'm sticking to GL 3.3, I don't want to go higher, mostly to stay compatible with most machines)
- A partial, semi-half game developer