2d Image Rotation lwjgl

I am making a simple game with lwjgl and slick-util but i’m still a big noob. Im having some trouble trying to rorate a simple 2d quad. These are my attempts:

First attempt:

	GL11.glRotatef(180, 0.0f, 0.0f, 1.0f);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glTexCoord2f(0, 0);
	GL11.glVertex2f(p.getPosition().x, p.getPosition().y);
	GL11.glTexCoord2f(1, 0);
	GL11.glVertex2f(p.getPosition().x + playerTexture.getTextureWidth(), p.getPosition().y);
	GL11.glTexCoord2f(1, 1);
	GL11.glVertex2f(p.getPosition().x + playerTexture.getTextureWidth(),
			p.getPosition().y + playerTexture.getTextureHeight());
	GL11.glTexCoord2f(0, 1);
	GL11.glVertex2f(p.getPosition().x, p.getPosition().y + playerTexture.getTextureHeight());
	GL11.glEnd();

Second attempt:

	GL11.glTranslatef(1.0f,0.0f, 0.0f);
	GL11.glRotatef(180, 0.0f, 0.0f, 1.0f);
	GL11.glBegin(GL11.GL_QUADS);
	GL11.glTexCoord2f(0, 0);
	GL11.glVertex2f(p.getPosition().x, p.getPosition().y);
	GL11.glTexCoord2f(1, 0);
	GL11.glVertex2f(p.getPosition().x + playerTexture.getTextureWidth(), p.getPosition().y);
	GL11.glTexCoord2f(1, 1);
	GL11.glVertex2f(p.getPosition().x + playerTexture.getTextureWidth(),
			p.getPosition().y + playerTexture.getTextureHeight());
	GL11.glTexCoord2f(0, 1);
	GL11.glVertex2f(p.getPosition().x, p.getPosition().y + playerTexture.getTextureHeight());
	GL11.glEnd();

But they both gave the same flashing image. My goal is to get the mouse to move the 2d image around.
I hope someone can help

http://pastebin.java-gaming.org/46e6f58145515

This is done in my engine, but it should be easy to directly convert to GL 1.1

It seems like you just keep rotating the matrix more and more. Are you calling glLoadIdentity() somewhere to reset the matrix? glPush/PopMatrix() is really useful too.

Also, you may already know this, but you won’t have to type GL11 every time if you use

import static org.lwjgl.opengl.GL11.*

Or whatever binding you’re using. Then you can just write glTranslatef() instead of GL11.glTranslatef().

Im so sorry for my late response :clue:i’ve been very busy with school and i will check right now

I’ve added glPop and now its kinda does start spinning but very weirdly like its being rotated by the 0 point on the screen.
I add glPop behind glLoadIdentity wich already was there.

Take in account, that’s deprecated OpenGL

You should submit vertices via buffers and rotate via matrices

Why do people always say that i just want to try I few things out and don’t want to start messing with that stuff

I think he said that to try to help you out…

I understand but i’m pretty sure that would take way longer.

Ok, are you on a time limit?

Because of this

Actually it’s the opposite.

Modifying/working with code you don’t really know how it works, it’s always a blind attempts after another.

You may solve this particular issue right now, but at the next problem you will be at the very same point, again and again.

I was in your position some time ago and I wish I had someone who told me the same I am telling you right now.

Exactly what elect said. The concepts are good to learn, you need them for many things. However, if you are learning them through something deprecated, you’re losing something extra that could be gained for no more effort. Think about it, either you could learn about modeling transforms through legacy openGL where you would learn both about modeling transforms and how old openGL works, OR you could learn about modeling transforms through modern openGL where you can learn both modeling transforms AND useful, modern openGL. The learning curve is identical, but what you get in the end is much different. Learning legacy openGL is essentially cutting away 50% of the gains from this endeavor for the same amount of effort as you would’ve put into learning modern openGL.

I understand. How would I go about doing this since i don’t have any knowledge on this? By the way, can anyone just please help me with my original question so i do have a little bit of acomplishment in my small little game :slight_smile:

I would expect your code to look something like this (adapted from your second attempt):


GL11.glTranslatef(p.getPosition().x, p.getPosition().y, 0.0f);
// GL11.glRotatef(180, 0.0f, 0.0f, 1.0f);
GL11.glScalef(
    playerTexture.getTextureWidth() * 0.5f,
    playerTexture.getTextureHeight() * 0.5f,
    1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(-1, -1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(+1, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(+1, +1);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(-1, +1);
GL11.glEnd();

This neatly defines your model-space vertices as a quad with [-1…+1] vertices centered at (0, 0), to make it less awkward to rotate and scale around that pivot point.
Starting from this, you should use the OpenGL matrix stack (or matrix uniforms when using shaders) to apply more model-space and view-space transformations, as is done above with glTranslatef to position the model at its position, and glScalef to scale it accordingly around its origin.

I guess your glRotatef(180 degrees) existed because your texture was rendered upside down.
I would fix this by inverting the y texture coordinate on your vertices instead.
(already done in the example above)

To understand what is going on, you gonna have to do a lot of reading about and playing around with two things:

  • linear algebra
  • the OpenGL rendering pipeline and its coordinate systems
  • (additionally, how exactly the OpenGL matrix stack works, if you stick with the fixed-function pipeline)

I recommend:

The crucial part here is understanding what OpenGL’s glTranslatef/glRotatef/glScalef and the other matrix stack operations are actually doing and to know which coordinate system you are operating in at any given point in your code. If you have that down, switching to shaders is straight-forward; at least for the transformation part.