使用旋转矩阵旋转纹理2d
Rotate Texture2d using rotation matrix
我想在 cocos2d-x(opengl es) 中旋转 2d 纹理,因为我搜索我应该使用旋转矩阵如:
(x = cos(deg) * x - sin(deg) * y y = sin(deg) *x + cos(deg) * y)
但是当我想实现这个公式时,我失败了,代码可能是这样的(我希望原始的 x 和 y 相同):我将代码更新为此,但仍然无法正常工作!
GLfloat coordinates[] = {
0.0f, text->getMaxS(),
text->getMaxS(),text->getMaxT(),
0.0f, 0.0f,
text->getMaxS(),0.0f };
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
GLfloat vertices[] = { rect.origin.x, rect.origin.y, 1.0f,
rect.origin.x + rect.size.width, rect.origin.y, 1.0f,
rect.origin.x, rect.origin.y + rect.size.height, 1.0f,
rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, 1.0f };
glMultMatrixf(vertices);
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glColor4f( 0, 0, 250, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glRotatef(-a, 0.0f, 0.0f, -1.0f);
glPopMatrix();
如果您在二维中使用 OpenGL,您仍然可以使用各种转换函数。 对于glRotate()
,您必须向其传递 (0,0,1) 轴:
glRotate(angle,0,0,1);
你似乎对OpenGL的几个方面感到困惑。目前你有这个:
glMultMatrixf(vertices);
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glColor4f( 0, 0, 250, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glRotatef(-a, 0.0f, 0.0f, -1.0f);
glPopMatrix();
这表面上是:
// reinterpret your vertices as a matrix; multiply the
// current matrix by the one formed from your vertices
glMultMatrixf(vertices);
// rotate by a degrees, then translate to p1
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
// bind the relevant texture, supply the vertices
// as vertices this time, supply texture coordinates
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// set a colour of (0, 0, 1, 1) — probably you
// wanted glColor4ub to set a colour of (0, 0, 250/255, 1)?
glColor4f( 0, 0, 250, 1);
// draw some geometry
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// perform the same rotation as before a second time;
// giving either a and -1, or -a and 1 would have been
// the opposite rotation
glRotatef(-a, 0.0f, 0.0f, -1.0f);
// remove the current matrix from the stack
glPopMatrix();
您可能想要的是:
// push the current matrix, so that the following
// transformations can be undone with a pop
glPushMatrix();
// rotate by a degrees, then translate to p1
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
// bind the relevant texture, supply the vertices
// as vertices this time, supply texture coordinates
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// set a colour of (0, 0, 250/255, 1)
glColor4ub( 0, 0, 250, 1);
// draw some geometry
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// remove the current matrix from the stack
glPopMatrix();
当前变换矩阵应用于所有几何图形。您不会标记它们应用于哪些几何图形,哪些不应用于几何图形。推送和弹出应该配对,并且是影响转换的方式,而不是在您想要时受到先前转换的影响。因此,您不需要手动执行反向旋转,并且在执行所有更改矩阵的操作之前需要推动。出于上述原因,我也将您切换到glColor4ub
- 您似乎使用无符号字节来推送颜色,但 OpenGL 使用从 0.0 到 1.0 的范围来表示颜色。 glColor4ub
将自动从前者映射到后者。
相关文章:
- 与互斥锁相比,旋转锁可以保证上下文切换
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- SFML纹理像播放器
- OpenGL大的3D纹理(>2GB)非常慢
- 绘制旋转的三角形
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 着色器纹理值与创建纹理时写入的值不同
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- OpenGL将纹理四边形渲染为(0,0)
- 将使用太多的纹理插值器 - 带旋转的着色器
- 在使用PI上的OpenGL ES绘制之前,如何旋转纹理
- 有没有办法在OpenGL上获取旋转的纹理
- 旋转纹理打开GL c++
- 旋转纹理 sfml 并将输出保存到文件
- 使用旋转矩阵旋转纹理2d
- 具有旋转纹理的圆柱体
- 一个在SDL2, c++中旋转纹理的方法
- 通过原始缓冲区数据缩放和旋转纹理到另一个纹理上