Jump to content
  • Advertisement
Sign in to follow this  
Dexario

OpenGL 2D - Rotation distorts sprite

This topic is 910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
 
I've been trying to create my own Sprite class in C++/OpenGL (and GLM/SFML/SOIL). I sorted the problem of rotating the sprite around its center but the problem I am now facing is the distortion of the sprite when rotated. Here are a few screenshots:
 
cf42911f34.jpg
 
1ded58bdd9.jpg
 
1ddf755258.jpg
 
Here is the code for the matrix tranformations:
 

	glm::mat4 model, projection, mvp;

	/* Model matrix */
	/* Translating the sprite to its position and scaling it */
	model = glm::translate(model, glm::vec3(displayPos.x / SCREEN_WIDTH * 2, -displayPos.y / SCREEN_HEIGHT * 2, 0.0f));
	model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, 0.0f));

	/* Rotating the sprite (we translate it to its origin, rotate it and translate it back to where it was) */
	glm::vec2 origin;
	origin.x = ((SCREEN_WIDTH / 2 - m_size.x / 2) / SCREEN_WIDTH * 2);
	origin.y = -((SCREEN_HEIGHT / 2 - m_size.y / 2) / SCREEN_HEIGHT * 2);

	model = glm::translate(model, glm::vec3(-origin.x, -origin.y, 0.0f));
	model = glm::rotate(model, m_rotation, glm::vec3(0.0f, 0.0f, 1.0f));
	model = glm::translate(model, glm::vec3(origin.x, origin.y, 0.0f));

	/* Projection matrix */
	projection = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f, 0.0f, 1.0f);

	mvp = model; //I do not multiply the projection mat because it causes the sprite to disappear
	glUniformMatrix4fv(glGetUniformLocation(m_texture->getShader()->getProgram(), "mvp"), 1, GL_FALSE, glm::value_ptr(mvp));

 
I understand that I should normally be using glm::ortho somewhere, though when I use it, it just causes the sprite to disappear.
Also note that when I put the dimensions of the screen to a square (like 800x800 instead of 800x600), it works fine. I tried using glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) but it doesn't change anything.
The image is a 320x320 square.
 
Thanks.

Edited by Dexario

Share this post


Link to post
Share on other sites
Advertisement

I believe it is because you are using a non-uniform scale in the model-view matrix.

 

model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, 0.0f));

 

also your comment...

//I do not multiply the projection mat because it causes the sprite to disappear

 

This is where the viewport correction scaling is supposed to happen.  This is where you should be looking to fix. 

 

Don't try to viewport correct your model in world space. 

Edited by Vulkanizer

Share this post


Link to post
Share on other sites

No, I commented out this line, nothing changed and in addition to that, m_scale.x = m_scale.y = 1.0f, which doesn't change anything.

Also, if I scale up/down and rotate, it behaves exactly like the case I presented but with a bigger or smaller image, so I don't think that is the source or the problem.

Edited by Dexario

Share this post


Link to post
Share on other sites

Without using projection matrix it's like you've send the data in clip coordinates, and with the w set to 1 which is the NDC coordinates, in this space the width of the screen is equal to the height of the screen, since the top right point on the screen is (1,1) and the down left is (-1,-1), and to display the image then these coordinates are scaled to fit the view port, so if you see a square on the screen in this coordinates that square's coordinates are actually not one for a square, so rotating it will cause the distortion.

 

What the projection matrix does is to scale the view space coordinates before transforming them to clip coordinates, this solves the problem, though you aren't using a projection matrix.

 

You can use a scale matrix instead of the projection matrix, scaling the x by h/w or y by w/h (w being the width of the screen and h being its height).

Share this post


Link to post
Share on other sites

What the projection matrix does is to scale the view space coordinates before transforming them to clip coordinates, this solves the problem, though you aren't using a projection matrix.

 
I'd like to use a projection matrix, it's just that it doesn't seem to work:

	/* Projection matrix */
	projection = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f);

	mvp = projection * model;
	glUniformMatrix4fv(glGetUniformLocation(m_texture->getShader()->getProgram(), "mvp"), 1, GL_FALSE, glm::value_ptr(mvp));

I've seen on the internet that this should do the trick, however, it just displays a white pixel at the top left of the window for me.

Edited by Dexario

Share this post


Link to post
Share on other sites

 

What the projection matrix does is to scale the view space coordinates before transforming them to clip coordinates, this solves the problem, though you aren't using a projection matrix.

 
I'd like to use a projection matrix, it's just that it doesn't seem to work:

	/* Projection matrix */
	projection = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f);

	mvp = projection * model;
	glUniformMatrix4fv(glGetUniformLocation(m_texture->getShader()->getProgram(), "mvp"), 1, GL_FALSE, glm::value_ptr(mvp));

I've seen on the internet that this should do the trick, however, it just displays a white pixel at the top left of the window for me.

 

projection = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f);

That will flip your image, which is you have face culling will result in the quad not being drawn.

 

Use:

projection = glm::ortho(0.0f, (float)SCREEN_WIDTH, 0.0f, (float)SCREEN_HEIGHT);

though i'd myself rather:

projection = glm::ortho(-(float)SCREEN_WIDTH/2.0f, (float)SCREEN_WIDTH/2.0f, -(float)SCREEN_HEIGHT/2.0f, (float)SCREEN_HEIGHT/2.0f);

Share this post


Link to post
Share on other sites

Using

projection = glm::ortho(0.0f, (float)SCREEN_WIDTH, 0.0f, (float)SCREEN_HEIGHT);

Doesn't display anything, though using

projection = glm::ortho(-(float)SCREEN_WIDTH/2.0f, (float)SCREEN_WIDTH/2.0f, -(float)SCREEN_HEIGHT/2.0f, (float)SCREEN_HEIGHT/2.0f);

Displays the image at the right position (or it seems to be) yet it is absolutely miniscule (about 2x2 pixels)

Share this post


Link to post
Share on other sites

The reason nothing is drawn with the first projection matrix might be the position of the quad, since it doesn't take the 0,0 of the scene as origin but the ((float)SCREEN_WIDTH/2.0f, (float)SCREEN_HEIGHT/2.0f), the reason things are miniscaled is that projection matrix scales the scene x by the h/w

Share this post


Link to post
Share on other sites

Position of the quad:

 

a0bfc0b527.png

 

Basically, the square is at the top left of the screen in the OpenGL coordinate system.

 

Is that right or was I supposed to center the quad (put its center at 0;0 in the OpenGL coordinate system) ?

Edited by Dexario

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!