Cropping out images for usage.

So i am recently transitioning to JOGL, and Im wandering how one goes about to crop/cut out textures out of a sheet of sprites to use for texture binding and everything else… I cant find any tutorials :persecutioncomplex:. Can anybody provide with a link or code?

when useing opengl just render with right texture coordinats

No thats ineffecient and slow because the sprite sheet is usually very big. I still need help :frowning:

What do you mean ineffecient? cutting an image into small part to several textures is worse.

There is nothing inefficient with using glTexCoords at all…

It’s most likely even faster since you don’t have to switch textures between every draw call.

Oh okay. Also, im using the glTexCoord2f to texturize different sections of the big sprite sheet. However, its really annoying manually specifying the top left, top right, blah blah coordinates for EVERY single type of little sprite image. Is there a better more humane way to do this?

You’re not getting away from texture coordinates, but there are tools you can use. I believe there’s a sprite sheet editor thingie for Slick.

oh okay cool. What is it? Lol in the meantime, i devised my own tool for automatic coordinate generator:

Oh here i finished it:

import org.newdawn.slick.opengl.Texture;


public class CropInfo {
	
	private float xd,yd,xwd,yhd;
	private int textureId;
	
	public CropInfo(Texture texture, int X, int Y, int W, int H){
		textureId = texture.getTextureID()-1;
		float texWidth = texture.getImageWidth();
		float texHeight = texture.getImageHeight();
		xd = X/texWidth;
		yd = Y/ texWidth;
		xwd = (X+W)/texWidth;
		yhd = (Y+H)/texHeight;
	}
	public int getTextureId() {
		return textureId;
	}
	public float getXD(){
		return xd;
	}
	public float getYD(){
		return yd;
	}
	public float getXWD(){
		return xwd;
	}
	public float getYHD(){
		return yhd;
	}
}

yeah and then all you need to do for the drawing is just



CropInfo ci = im.getCropInfo(blah blah....

glBegin(GL_QUADS);
	glTexCoord2f(ci.getXD(),ci.getYD());// Upper-left
	glVertex2i(100, 100); 
	glTexCoord2f(ci.getXWD(), ci.getYD());// Upper-right
	glVertex2i(500, 100); 
	glTexCoord2f(ci.getXWD(), ci.getYHD());// Bottom-right
	glVertex2i(500, 500); 
	glTexCoord2f(ci.getXD(), ci.getYHD()); // Bottom-left
	glVertex2i(100, 500);

What, what? :persecutioncomplex:

There is an array with the CropInfo so if you wanna get the crop subimage info for say texture # 5, you go to the array and retrieve the 4th index because JOGL starts the first texture ID with number 1. In other words, arrays are 0,1,2,3,4… But the JOGL texture ID itself goes 1,2,3,4,5. So thats why i subtracted one from the texture ID, so i can call it from an array neatly.

It is not safe to make any assumptions on the texture IDs you get from OpenGL.

Further, what if you delete and create new textures. The handles will be more and more ‘out of sync’ with your array indices.

I never thought about that. How can i resolve it? Now it wont be a problem since i wont be creating/deleting textures, but later it might…

Just maintain an ArrayList of Texture objects. I doubt whatever order you might need them in is always going to be the same as the sort order of their internal identifiers. That Id is opaque, you should not be using it as anything but a handle. If you need meaningful names for the textures, use something like Map<String,Texture> or assign them to fields.

Also i have another question regarding images and textures. In normal Java, i usually create a BufferedImage in which I can draw on background images, items, props, whatever, and then draw that bufferedimage on the screen. I want to do the same on JOGL. How do i make a texture in which i can draw on?

Normally in openGL, everything are drawn into static GL class.

So how does this translate into drawing on textures?