Clipping part of a texture

Started by Taien, April 28, 2013, 02:26:19

Previous topic - Next topic

Taien

Hey guys, first post :) 

So I've been working on a 2D game to "get my feet wet," so to speak, and I'm having a bit of trouble with textures.  I understand that lwjgl can only handle square textures with a size that is an exponent of 2.  So in order to get accurate hit detection on objects (without the empty space surrounding the object counting as part of it) I'm trying to draw textures with that section clipped out, which in theory shouldn't be too hard.  This seems easier to me than trying to compare each pixel to determine touches.

My problem is this:  when I draw the texture and only draw to a certain clipping of it, I'm clipping the stuff from the wrong corner.  I realize of course that this is entirely because I'm new to this point-drawing stuff and I'm clipping the wrong points in order to get my final product.  But no matter how much I mess with it, I can't get it to clip the correct part of the texture. 

Here's my initGL (I'm drawing with 0,0 as the top left corner):

{
		glEnable(GL_TEXTURE_2D);
		glMatrixMode(GL_PROJECTION); //projection mode (stretches stuff)
		glLoadIdentity(); //clear projection matrix
		glOrtho(0,Display.getWidth(),Display.getHeight(),0,-1,1); //orthographic view (2d) ...x,width,y,height,z,depth
		glMatrixMode(GL_MODELVIEW); //modelview mode (draws shapes)
		glClearColor(0f,0f,0f,1f); //sets transparency color to full black
		glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glDisable(GL_DEPTH_TEST); //disregards information pertinent to 3D only
	}


Here's the method I'm trying to use to clip with (currently, I've messed with it a lot so it might be completely wrong):
public static void rect2Dclipped(float x, float y, float w, float h, Texture t)
	{
		glPushMatrix(); //creates a matrix for this code alone instead of all code being in the same matrix
		{
			glTranslatef(x,y,0); //shifts drawing
			glRotatef(0,0,0,1); //degrees, axes to rotate on
			glColor4f(1f,1f,1f,1f); //sets color for object (0-1) floating point values
			
			t.bind();
			
			float wdiff = t.getImageWidth() - w;
			float hdiff = t.getImageHeight() - h;
			float wpercent = w / t.getImageWidth();
			float hpercent = h / t.getImageHeight();
			
			glBegin(GL_QUADS); //begin 4 point drawing
			{
				glTexCoord2f(1f-wpercent,1f-hpercent);
				glVertex2f(wdiff,hdiff);
				glTexCoord2f(1f-wpercent,hpercent);
				glVertex2f(wdiff,h);
				glTexCoord2f(wpercent,hpercent);
				glVertex2f(w,h);
				glTexCoord2f(wpercent,1f-hpercent);
				glVertex2f(w,hdiff);
			}
			glEnd(); //end drawing
		}
		glPopMatrix();  //populates the matrix into the main matrix
	}


As you might be able to tell from my definitely incorrect code, I'm trying to clip from the top and right of the texture.  Meaning I want stuff on the left and bottom to be clipped last as I clip more of the texture away.    I've tried rearranging the points a lot of different ways but I'm just not getting this.  I'd appreciate any insight someone can give me into how I'm doing this wrong.  Thanks in advance for any replies.

quew8

Why don't you try:

glTexCoord2f(1f-wpercent, 1f-hpercent); //Bottom left corner of desired texture region
...
glTexCoord2f(1f-wpercent, 1f); //Top left
...
glTexCoord2f(1f, 1f); //Top right
...
glTexCoord2f(1f, 1f-hpercent); //Bottom right
...


I think this is the order you wanted them in but I labeled them anyway. Btw I hope you're aware that the size of the quad you draw does not have to relate to the size of the desired texture in any way. It's just that I found your vertex coords to be most confusing in relation to the x, y, w, h, params you pass in.

Taien

I'll try that, thanks :)  And yeah I realize they don't have anything to do with each other...basically I'm just trying to make it so I can define a width for a certain kind of enemy so it'll draw only that much of the texture from the left/bottom sides, and the enemy will be the proper size for hit detection.  Also I want to use the same code to draw the HUD lifebars, because they aren't just rectangular, they're all fancy-shaped so I want to be able to clip the section that isn't filled instead of distorting the texture.