Textured Quad Rotation

Started by Cornix, November 15, 2013, 20:23:08

Previous topic - Next topic

Cornix

Hi there.
I am going crazy right now.
I have a piece of code which I made a good while ago and which, as far as I remember, worked perfectly fine.
Now as I am going back to it I realize that it doesnt work anymore and I just cant figure out what is wrong.

The code was written with OpenGL vesion 1.1 and was supposed to just draw a textured quad across the screen.
public void draw() {
		GL11.glBegin(GL11.GL_QUADS);

		GL11.glVertex2f		(0, 0);
		GL11.glTexCoord2f	(0, 0);
		
		GL11.glVertex2f		(0, 240);
		GL11.glTexCoord2f	(0, 1);
		
		GL11.glVertex2f		(320, 240);
		GL11.glTexCoord2f	(1, 1);
		
		GL11.glVertex2f		(320, 0);
		GL11.glTexCoord2f	(1, 0);

		GL11.glEnd();
	}

The vertices are 1) upper-left, 2) lower-left, 3) lower-right and 4) upper-right.
I use glOrtho with the parameters 320 for width and 240 for height on the projection matrix. (0, 0) being the top left corner!

What the code does is draw a quad on the screen, but, rotated in the wrong direction. Its rotated counter-clockwise by 90°, I am very sure it wasnt like this before.
The code for loading the texture looks somewhat like this:
InputStream stream = new FileInputStream(path);
		PNGDecoder decoder = new PNGDecoder(stream);
		width = getPowerOfTwo(decoder.getWidth());
		height = getPowerOfTwo(decoder.getHeight());
		
		ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.nativeOrder());
		decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.BGRA);
		while (buffer.remaining() > 0){
			buffer.put(Byte.MIN_VALUE);
		}
		buffer.flip();
		stream.close();
		
		glName = GL11.glGenTextures();
		
		int tex2D = GL11.GL_TEXTURE_2D;
		
		GL11.glEnable(tex2D);
		GL11.glBindTexture(tex2D, glName);
		GL11.glTexParameteri(tex2D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
		GL11.glTexParameteri(tex2D, GL12.GL_TEXTURE_MAX_LEVEL, 0);
		GL11.glTexImage2D(tex2D, 0, GL11.GL_RGBA, width, height, 0, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buffer);
		GL11.glTexParameteri(tex2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
		GL11.glTexParameteri(tex2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
		GL11.glTexParameteri(tex2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
		GL11.glTexParameteri(tex2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);

The image used is 16x16 pixels.

The problem is that there is just sooo many places where this can go wrong and I just cant think of all of them right now.
If anybody has an idea what I should try or where I should check this would be greatly appreciated.

Thank you very much.

quew8

You are specifying the vertex position before the tex coord. ie calling glVertex() before glTexCoord(). So the first tex coord is going with the second vertex, the second tex coord is going with the third vertex. Since you defined it counter clockwise like a good little OpenGL er, the texture is rotated 90 degrees counter clockwise.

Cornix

Damn it, you are right!

I had just mixed up the order of commands, everything else was exactly the same as the old version. And I wondered what was going on, it was the very same lines of code!

Thank you very much, what a stupid mistake.