无法在鼠标单击 OpenGL 时围绕其自身中心旋转对象

Can't rotate object around its own center on mouse click OpenGL

我成功地旋转了我的对象,但我需要右键单击该对象以停止其旋转。我不知道如何使对象绕其中心旋转然后停止,我将附上代码以查看此形状到底发生了什么。我认为问题出在 spinDisplay() 函数上……问题是我需要在单击鼠标左键时围绕它的中心旋转,而在单击鼠标右键时我应该更改对象的颜色…… .

#include <stdlib.h>
#include <math.h>
#include "dependente\freeglut\freeglut.h"
#include "dependente\glfw\glfw3.h"
#include <stdio.h> //incluziuni librarii

float ORG[3] = { 0,0,0 };
static GLfloat spin = 0.0;
GLfloat viewangle = 0, tippangle = 0, traj[120][3]; //variabila pentru unghi camera

GLfloat d[3] = { 0.1, 0.1, 0.1 }; //vector directie

GLfloat  xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
bool draw_triangle = false; //variabila desenat figuri 
bool draw_square = false;
bool draw_decagon = false;


void Triangle(void) //draw the triangle shape
{
    glBegin(GL_TRIANGLE_FAN);//triangles have a common vertex, which is the central vertex
    glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);   //V0(red)
    glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);   //V1(green)
    glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f);   //V2(blue)
    glEnd();
}
void Square(void) {
    glBegin(GL_QUADS);
    glVertex2f(-1.0f, 1.0f); // top left
    glVertex2f(1.0f, 1.0f); // top right 
    glVertex2f(1.0f, -1.0f); // bottom right
    glVertex2f(-1.0f, -1.0f); // bottom left
    glEnd();
}
void Decagon(void) //draw the decagon shape
{
    glBegin(GL_POLYGON);
                glColor3f(0.0f, 0.0f, 1.0f);
                glVertex3f(0.72f,0.8f, 0.0f); //a1
                glVertex3f(0.52f,   0.8f,0.0f);  //z
                glVertex3f(0.35f,   0.64f, 0.0f); //b1
                glVertex3f(0.3f,   0.48f, 0.0f); //d1
                glVertex3f(0.35f,   0.3f, 0.0f); //e1
                glVertex3f(0.52f, 0.16f, 0.0f); //l1
                glVertex3f(0.72f, 0.16f, 0.0f); //m1
                glVertex3f(0.9f, 0.3f, 0.0f); //o1
                glVertex3f(0.95f, 0.48f, 0.0f); //p1
                glVertex3f(0.9f, 0.64f, 0.0f); //c1
                glScalef(10, 10, 10);
                glTranslatef(1, 2, 3);
    glEnd();
}

void Keyboard(unsigned char key, int x, int y) //press a key to perform actions
{
    switch (key) {

    case 'd': d[0] += 0.1;  break; //camera right
    case 'a': d[0] -= 0.1;  break; //camera left
    case 'w': d[1] += 0.1;  break; //camera up 
    case 's': d[1] -= 0.1;  break; //camera down 
    case 'm': d[2] += 0.1;  break; //magnify
    case 'n': d[2] -= 0.1;  break; //minify
    case 't': draw_triangle = true; draw_decagon = false;   break; //draw pyramid when key is pressed
    case 'q': draw_square = true; draw_decagon = false; draw_triangle = false; break; //draw cube when key is pressed
    case 'l': draw_decagon = true; draw_triangle = false; draw_square = false; break; //draw prism when key is pressed

    case 'x': xAngle += 5;  break; //modify x axis angle
    case 'y': yAngle += 5;  break; //modify y axis angle
    case 'z': zAngle += 5;  break;  //modify z axis angle

    default: printf("   Keyboard %c == %d", key, key); //see what key it's pressed
    }

    glutPostRedisplay();
}

void spinDisplay() //here it's the problematic function
{
    spin = spin + 0.1;
    if (spin > 360.0)
    {
        spin = 0.0;
    }
    glutPostRedisplay();
}
void mouse(int buton, int state, int x, int y)
{
    switch (buton) {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(spinDisplay);
        break;
    case GLUT_RIGHT_BUTTON: //here I don't know how to change the color of the shape
        glutIdleFunc(NULL);
    default:glutIdleFunc(NULL);

        break;
    }
}
void redraw(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    glLoadIdentity();

    glTranslatef(0, 0, -3);
    glRotatef(tippangle, 1, 0, 0);  // Up and down arrow keys 'tip' view.
    glRotatef(viewangle, 0, 1, 0);  // Right/left arrow keys 'turn' view.

    glDisable(GL_LIGHTING);


    glPushMatrix();
    glTranslatef(d[0], d[1], d[2]);    // Move box down X axis.
    glScalef(0.7f, 0.7f, 0.7f); //increase the object size
    glRotatef(zAngle, 0, 0, 1);
    glRotatef(yAngle, 0, 1, 0);
    glRotatef(xAngle, 1, 0, 0);
    glRotatef(spin, 0.0, 0.0, 1.0);

    if (draw_triangle)
        Triangle();

    if (draw_decagon)
        Decagon();
    if (draw_square)
        Square();

    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(900, 600);
    glutInitWindowPosition(300, 300);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);

    glutCreateWindow("Figure Rotation");
    glutDisplayFunc(redraw);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glClearColor(0.1, 0.0, 0.1, 1.0);

    glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix, matrix that represents your camera's lens (aperture, far-field, near-field, etc).
    gluPerspective(60, 1.5, 1, 10); //set up a perspective projection matrix
    glMatrixMode(GL_MODELVIEW); //specify which matrix is the current matrix,matrix that represents your camera (position, pointing, and up vector).
    glutMainLoop();

    return 1;
}

仅根据您的代码,主要问题在于 Decagon() 的形状顶点定义。 由于这样的顶点不是在形状本身的中心定义的,而是在右上角定义的,因此它不会围绕自身旋转但似乎围绕中心旋转,尽管您的矩阵乘法序列工作正常。

为简单起见,我将其可视化为沿 xy 平面将其置于 0,0 中心,然后定义其形状的右半部分,然后将其镜像回左侧。您可以利用 - 减号。隐含地利用在 NDC (Normalizd Device Coordinate) space.

中定义形状

注意:与您最初定义的比率不完全相同,但可以让您了解一下。你可以试着把下面的换成你的,然后它应该绕着自己旋转。

    glBegin(GL_POLYGON);
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(0.25f, 0.5f, 0.0f);
        glVertex3f(0.45f, 0.30f, 0.0f);
        glVertex3f(0.55f, 0.0f, 0.0f);
        glVertex3f(0.45f, -0.30f, 0.0f);
        glVertex3f(0.25f, -0.5f, 0.0f);
        glVertex3f(-0.25f, -0.5f, 0.0f);
        glVertex3f(-0.45f, -0.30f, 0.0f);
        glVertex3f(-0.55f, 0.0f, 0.0f);
        glVertex3f(-0.45f, 0.30f, 0.0f);
        glVertex3f(-0.25f, 0.5f, 0.0f);
        //glScalef(10, 10, 10);  // this won't have any effect on result
        //glTranslatef(1, 2, -3);// the same
    glEnd();

这里有 2 个选项

  1. 完全改变顶点定义(只有Decagon与上面相对于原点的相似)。其他形状已经好了,是相对于原点定义的。
  2. 无论您定义的形状的顶点如何,都要仔细确定形状的原点。在所有其他操作之前,首先使用这样的位置将形状转换回矩阵操作的一部分(请继续阅读以了解原因)。

绕自身旋转的概念

围绕自身旋转的概念就是我们需要依次进行以下操作

  1. 规模(在这种情况下我们没有)
  2. 旋转
  3. 翻译

缩放虽然我们在这种情况下没有,但应该放在最后,否则可能会影响其他两个操作。

如果我们先平移到任意位置,那么旋转将围绕该点发生。其实旋转是相对于原点的0,0,因此我们只需要在继续之前无论如何将物体放回原点,然后我们就可以旋转,平移到它应该的位置,并且规模.

让我们看看你的矩阵乘法顺序

    glScalef(0.7f, 0.7f, 0.7f); //increase the object size
    glTranslatef(d[0], d[1], d[2]);    // Move box down X axis.
    glRotatef(zAngle, 0, 0, 1);
    glRotatef(yAngle, 0, 1, 0);
    glRotatef(xAngle, 1, 0, 0);
    glRotatef(spin, 0.0, 0.0, 1.0);

这意味着我们按顺序执行以下操作

  • 绕z轴旋转spin角度
  • 绕x轴旋转xAngle角度
  • 绕y轴旋转yAngle角度
  • 绕z轴旋转zAngle角度

虽然我们可以把第一个和最后一个结合起来,但是没关系。

Also you might want to further look at Euler Angles when we rotate around 3 cardinal axes like this, it can lead to Gimbal lock problem but it can be solved by limiting angles user can rotate around a certain axis.

顺序是对的。这被翻译成数学术语 S * T * Rz * Ry * Rx * Rspin ,您可以在其中看到它与代码中的顺序相反。 Rspin 先发生,然后 Rx 然后依此类推。

现在,如果 Decagon 形状定义为 而非 相对于原点,而是以向右平移的方式定义,会发生什么情况。

采用我的顶点定义,但是 + 0.55f 对于所有 x 位置,我们将有

    glBegin(GL_POLYGON);
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(0.80f, 0.5f, 0.0f);
        glVertex3f(1.0f, 0.30f, 0.0f);
        glVertex3f(1.10f, 0.0f, 0.0f);
        glVertex3f(1.0f, -0.30f, 0.0f);
        glVertex3f(0.80f, -0.5f, 0.0f);
        glVertex3f(0.30f, -0.5f, 0.0f);
        glVertex3f(0.10f, -0.30f, 0.0f);
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.1f, 0.30f, 0.0f);
        glVertex3f(0.30f, 0.5f, 0.0f);
    glEnd();

如果您将上面的代码替换为您的顶点定义,那么它就不会再围绕自身旋转了。但是我们知道 x 轴需要 -0.55f 才能使这个形状回到原点,因此如果我们添加 glTranslatef(-0.55f, 0.0f, 0.0f) 作为第一个要执行的操作,那么它的工作原理是一样的。

我们会

    glScalef(0.7f, 0.7f, 0.7f);
    glTranslatef(d[0], d[1], d[2]);
    glRotatef(zAngle, 0, 0, 1);
    glRotatef(yAngle, 0, 1, 0);
    glRotatef(xAngle, 1, 0, 0);
    glRotatef(spin, 0.0, 0.0, 1.0);
    glTranslatef(-0.55f, 0.0f, 0.0f);  // <------ add this

简而言之,先将目标对象平移到原点,然后再旋转(围绕自身),然后像以前一样进行适当的顺序。

If you desire to have such object to be located at the location you've defined the shape's vertices i.e. it's to the right +0.55f along x-axis and still rotate around itself. Then you use glTranslatef(d[0] + 0.55f, d[1], d[2]) instead.

进一步说明

  1. 最后两个 gl 操作 glScalef()glTranslatef() 不会有任何效果,因为您已经绘制了形状。当您调用 glLoadIdentity().
  2. 时,这两个操作每帧都会被丢弃
  3. 请注意,源代码仍然基于OpenGL 的固定功能管道。您可能还想看看现代可编程管道。这将允许您更灵活地控制 虚拟 相机,因此矩阵操作更加清晰,并且在我们需要四处移动时与对象本身分离。所以这将使矩阵运算更容易掌握和理解。

编辑 额外控制和满足应用要求如下

  • 左键单击无限旋转,然后再次左键单击停止
  • 右键单击以循环显示渲染形状的颜色

我们必须有控制标志,以及我们可以在任何帧时间更改的信息,如下所示。

bool isSpinning = false;
#define NUM_COLOR 4
int sCurrColor = 0;
GLfloat sColor[NUM_COLOR][3] = {
    {1.0f, 1.0f, 1.0f},
    {1.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 1.0f}
};

所以对于颜色,我们有白色、红色、蓝色和绿色。共有 4 种颜色,每种颜色有 3 个 RGB 分量值。我们从索引 sCurrColor 中看到的白色开始。

现在你的 mouse() 函数看起来像这样

void mouse(int buton, int state, int x, int y)
{
    switch (buton) {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN && !isSpinning)
            isSpinning = true;
        else if (state == GLUT_DOWN && isSpinning)
            isSpinning = false;
        break;
    case GLUT_RIGHT_BUTTON: //here I don't know how to change the color of the shape
        if (state == GLUT_DOWN)
            sCurrColor = (sCurrColor + 1) % NUM_COLOR;
        break;
    default:glutIdleFunc(NULL);

        break;
    }
}

我们优化了移动 glutIdleFunc(spinDisplay); 以在 main() 函数内部调用 glutMainLoop()。根据您的要求,我们不必每帧都更改它。

因此,spinDisplay()现在更改为

void spinDisplay() //here it's the problematic function
{
    if (isSpinning)
    {
        spin = spin + 3.0;
        if (spin > 360.0)
        {
            spin = 0.0;
        }
    }
    glutPostRedisplay();
}

Probably better to change the name of function to something like display() as it's more generic to not confuse that we have to spin everytime. Anyway, I didn't change this for the sake of brevity and consistency of your code.

现在最后一部分是插入 sColor 以供所有这些形状在渲染中使用。例如 Decagon 你可以这样做

glColor3f(sColor[sCurrColor][0], sColor[sCurrColor][1], sColor[sCurrColor][2]);

如果您希望通过右键单击以循环显示颜色来获得相同的效果,这对于其他形状也是一样的。