gpt4 book ai didi

c++ - 无法使用 opengl 正确旋转 2D 对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:36 24 4
gpt4 key购买 nike

我正在使用 opengl、glfw 和 glew。当用户分别按下左右箭头时,我想旋转飞船的尖端而不移动整个飞船。我试过使用 gltransform glrotate,它确实会旋转,但它也会移动我的宇宙飞船。空格移动的输出

objects objects::rotateSpaceship(GLFWwindow * window, objects & sp)
{
GLfloat spaceship_angle = sp.getAngle();

GLfloat newAngle = sp.getAngle() + 0.5;
sp.setAngle(newAngle);

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);

GLfloat spaceShipRightLine[] =
{
x, y + 80, 0,
x - 50, y - 50, 0
};


GLfloat spaceShipLeftLine[] = {
x, y + 80, 0,
x + 50, y - 50, 0
};

GLfloat spaceShipMiddleLine[] = {
sp.x - 30 ,sp.y,0,
sp.x + 35,sp.y,0
};


glPushMatrix();

glTranslatef(0.0, 0.01, 0.0); // 3. Translate to the object's position.

glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.

glTranslatef(0.0, 0.01, 0.0); // 1. Translate to the origin.

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glPopMatrix();

// Swap front and back buffers
glfwSwapBuffers(window);

// Poll for and process events
glfwPollEvents();
}

glfwTerminate();
return sp;
}

最佳答案

请注意,glBegin/glEnd 绘制的那幅画sequences 和 fixed function matrix stack 几十年来一直被弃用。了解 Fixed Function Pipeline并查看 Vertex SpecificationShader最先进的渲染方式。


I want to rotate the tip of the spaceship without moving the whole spaceship [...]

首先你必须定义支点,也就是宇宙飞船的“尖端”:

float pivot_x = sp.x;
float pivot_y = sp.y + 80.0f;

要围绕一个枢轴旋转,您必须定义一个模型矩阵,该矩阵由反转的枢轴位移,然后旋转 (glRotatef) 并最终转换回枢轴 (glTranslate):

glTranslatef(pivot_x, pivot_y, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-pivot_x, -pivot_y, 0);

最终代码可能是这样的:

float pivot_x = x;
float pivot_y = y + 80.0f;

glPushMatrix();

glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);

glPopMatrix();

关于c++ - 无法使用 opengl 正确旋转 2D 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54026714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com