Reputation: 23
I think it is common knowledge for anyone who has used Box2D and OpenGL that the origin of Box2D bodies is at the center of the body and for OpenGL textures, it is at the bottom left corner. I am using Box2D to create a body and attaching an OpenGL texture to it with the function,
glTranslatef(Player_Body->GetPosition().x, Player_Body->GetPosition().y, 0.0f)
However, since the GetPosition()
returns the origin, my texture is attached to the body with its bottom left corner at the body's center. How do I change the origin of the OpenGL texture to the center of the texture?
I don't think this has anything to do with World to Screen conversions. Not yet, at least. Because I am not performing any physics, just displaying a static body. Currently, the ratio is Meters:Pixels -> 1:1
(I know this is bad practice, but it is just a temporary test). My program uses GLFW as the windowing system. I previously used GLUT, it displayed correctly, but I had a monitor device context problem with GLUT so I'm using GLFW. The OpenGL code for drawing and transforming remains the same, however. So does the Box2D code. So, how do I solve this?
P.S. : I am not posting any code because this is more of a logic related question. But I will post if required.
Upvotes: 0
Views: 386
Reputation: 14688
Don't think about it as the origin of the texture, but rather the origin of the vertices making the rectangle that displays that texture.
Texture coordinates in OpenGL are defined by the specification to be in a coordinate space where the lower-left corner of the texture is the origin. What you can change is your vertices in object space. Without code, I'm going to assume your vertices are defined as (0, 0)
to (w, h)
when you create them. In object space, this means that one corner of your rectangle is at the origin and it's all in one quadrant. Your glTranslatef
call is what takes the vertices from object space to world space.
What you should do instead is shift your vertices so that the center of the rectangle is in the origin of object space. i.e. from (-w/2, -h/2)
to (w/2, h/2)
, that way when you translate the vertices to object space, the origin is at the center.
Here's a visualization of what I'm talking about (in object space):
| |
|***** |
|***** |
|***** *****
----------------- < current ------*****------ < new
| *****
| |
| |
| |
Upvotes: 0