Java opengl旋转纹理偏离中心
我写了一些代码,让一个普通的2d框面对鼠标。它围绕中心旋转的很好,一切都很好,但是当我在盒子上放置纹理时,它就不再围绕中心旋转了 守则:Java opengl旋转纹理偏离中心,java,opengl,Java,Opengl,我写了一些代码,让一个普通的2d框面对鼠标。它围绕中心旋转的很好,一切都很好,但是当我在盒子上放置纹理时,它就不再围绕中心旋转了 守则: float imgWidth = texture.getImageWidth()*scale; float imgHeight = texture.getImageHeight()*scale; glLoadIdentity(); texture.bind(); glTranslatef(x, y, 0);
float imgWidth = texture.getImageWidth()*scale;
float imgHeight = texture.getImageHeight()*scale;
glLoadIdentity();
texture.bind();
glTranslatef(x, y, 0);
glRotated(rotation - 90, 0, 0, 360);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-imgWidth, -imgHeight);
glTexCoord2f(1, 0);
glVertex2f(imgWidth, -imgHeight);
glTexCoord2f(1,1);
glVertex2f(imgWidth, imgHeight);
glTexCoord2f(0, 1);
glVertex2f(-imgWidth, imgHeight);
glEnd();
答案很简单,但有一个复杂的背景,必须理解 OpenGL始终不围绕其中心旋转某些对象,而是以点(0;0)为中心。 这可能是一个问题,因为如果你在某处平移你的对象,然后旋转它,它将不会围绕其中心旋转,而是围绕(0;0)点(原点)旋转,从而产生一个大的旋转,我会说,作为一个围绕太阳的行星 OpenGL也使用matrix,非常野蛮的简化意味着操作是自下而上执行的
// store the current model matrix
GL11.glPushMatrix();
// bind to the appropriate texture for this image
this.texture.bind();
// translate to the right location and prepare to draw
GL11.glColor3f(1, 1, 1);
GL11.glTranslated(x + (this.texture.getImageWidth() / 2), y + (this.texture.getImageHeight() / 2), 0);
GL11.glRotated(this.angle, 0, 0, 1);
GL11.glTranslated(-this.texture.getImageWidth() / 2, -this.texture.getImageHeight() / 2, 0);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, this.texture.getHeight());
GL11.glVertex2f(0, this.texture.getImageHeight());
GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight());
GL11.glVertex2f(this.texture.getImageWidth(), this.texture.getImageHeight());
GL11.glTexCoord2f(this.texture.getWidth(), 0);
GL11.glVertex2f(this.texture.getImageWidth(), 0);
}
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
这意味着首先我要移动纹理,使其中心位于(0;0),这意味着将其向后平移一半的维度。
然后我旋转它,这是关键点,因为你使用了一种奇怪的方式来旋转它,可能是这里的问题,看看javadoc:
SPECIFICATION
void glRotated( GLdouble angle,<br>
GLdouble x,<br>
GLdouble y,<br>
GLdouble z )<br>
void glRotatef( GLfloat angle,<br>
GLfloat x,<br>
GLfloat y,<br>
GLfloat z )<br>
PARAMETERS<br>
angle Specifies the angle of rotation, in degrees.
x, y, z<br>
Specify the x, y, and z coordinates of a vector, respectively.
DESCRIPTION<br>
glRotate produces a rotation of angle degrees around the<br>
vector (x,y,z).
规范
void glRotated(GLdouble angle,
GLX双倍,
gly,
glz)
无效glRotatef(GLfloat角度,
GLX浮动,
gly,
glz)
参数
角度指定旋转角度,单位为度。
x、 y,z
分别指定向量的x、y和z坐标。
说明
glRotate将围绕
向量(x,y,z)。
首先,所有x、y、z值应介于0和1之间,如果要旋转2d图像,则应使用z轴,因此第三个参数将为1,这意味着您正在围绕单位向量z旋转图像。
角度应为度,可以为正或负
尝试根据文档更改代码,您的问题就会得到解决。
同样,使用四边形绘制一个2倍比例的四边形,从-imageWidth开始到+imageWidth,这意味着宽度的2倍…因此,在本例中,如果注释掉对.Bind()的调用,它会起作用吗?.Bind()中有什么?