2D opengl旋转会导致精灵变形 [英] 2D opengl rotation causes sprite distortion
问题描述
我对使用OpenGL ES 2.0非常陌生.我也使用iPhone和GLM数学库.正如我所引用的,我经常使用本教程: http: //tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/但我发现很难找到2D OpenGL教程.
I am quite new to using OpenGL ES 2.0. Also im using an iPhone and the GLM maths library. As I reference I have been using this tutorial a lot: http://tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/ But I have found it difficult to find a 2D OpenGL tutorial.
我正在尝试旋转2D精灵,但是图像失真了. 例如,旋转角度为0度: http://i.imgur.com/yBTN2ST.png并以45度的旋转角度: http://i.imgur.com/cY5IJcg.png
I am trying to rotate a 2D sprite but the image is being distorted. For example at a rotation angle of 0 degrees: http://i.imgur.com/yBTN2ST.png and at a rotation angle of 45 degrees: http://i.imgur.com/cY5IJcg.png
在我的Sprite班中
glm::mat4 projection = glm::ortho(0.0f, 480.0f, 0.0f, 320.0f);
glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 newModel = glm::rotate(glm::mat4(), 0.0f, glm::vec3(0, 0, 1));
glUniformMatrix4fv(modelUniform, 1, GL_FALSE, value_ptr(newModel));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
在我的顶点着色器中
vec4 newPosition = vec4(position + offset, 0.0, 1.0);
gl_Position = model * projection * newPosition;
我认为部分问题是图像旋转时没有考虑长宽比,我不确定该如何解决.
I think that part of the problem is that the image is being rotated without considering the aspect ratio im not really sure how to fix this.
谢谢
推荐答案
我认为您以错误的顺序应用了转换.
I think you are applying your transformations in the wrong order.
代替:
gl_Position = model * projection * newPosition;
尝试:
gl_Position = projection * model * newPosition;
更好(避免将两个4x4矩阵相乘):
Better still (to avoid multiplying two 4x4 matrices together):
gl_Position = projection * ( model * newPosition );
甚至更好:在您的应用程序中将矩阵相乘,并在着色器中进行一次矩阵矢量相乘.您可能还应该在矩阵中包含翻译.
Even better: Multiply your matrices in your application and do a single matrix-vector multiplication in your shader. You should probably also include the translation in the matrix.
这篇关于2D opengl旋转会导致精灵变形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!