如何将计算机绘图旋转 90 度

Posted

技术标签:

【中文标题】如何将计算机绘图旋转 90 度【英文标题】:How to rotate a computer drawing in 90 degrees 【发布时间】:2014-11-15 13:48:43 【问题描述】:

我在空白窗口中创建了兔子和鸭子图像。我的计划是显示两次:一次单独显示,另一张像它一样旋转 90 度。我已经尝试第二次创建图像并通过更改值来转动图像,但是很困难并​​且根本无法工作。需要使用哪些轴在平面中旋转图像以及完成它的正确方法。

void myInit(void)
        glClearColor(1.0, 1.0, 1.0, 0);  // the background is white
        glColor3f(0.0f, 0.0f, 0.0f);  // set drawing color
        gluOrtho2D(0.0, (GLdouble) screenWidth, 0.0, (GLdouble) screenHeight);
    

void drawBunny()
    glClear(GL_COLOR_BUFFER_BIT);
    // draw the outline of box (bunny)
    glLineWidth(2);
    glBegin(GL_LINE_LOOP);
        glVertex2i(50,50);
        glVertex2i(150,50);
        glVertex2i(150,100);
        glVertex2i(50,100);

    glEnd();

    //draw bunny tail
    glLineWidth(1);
    glBegin(GL_LINE_STRIP);
     glVertex2i(50,50);
     glVertex2i(50,35);//2nd wider top/bottom
     glVertex2i(70,35);//1st- shrink tail left/right
    glVertex2i(70,50);//1st- shrink tail left/right
    glEnd();

    // draw first ear
    glBegin(GL_LINE_LOOP);
        glVertex2i(175,85);
        glVertex2i(175,100);
        glVertex2i(150,100);
        glVertex2i(150,85);
    glEnd();

    //draw second ear
    glBegin(GL_LINE_LOOP);
        glVertex2i(175,70);
        glVertex2i(175,100);
        glVertex2i(150,100);
        glVertex2i(150,70);
    glEnd();

    // draw the head
    glBegin(GL_LINE_LOOP);
        glVertex2i(150,100);
        glVertex2i(150,110);
        glVertex2i(125,110);
        glVertex2i(125,100);
    glEnd();

    // draw first feet
    glBegin(GL_LINE_LOOP);
        glVertex2i(110,60);
        glVertex2i(110,75);
        glVertex2i(30,75); //decrease value increase feet
        glVertex2i(30,60);
    glEnd();

    //draw second feet
    glBegin(GL_LINE_LOOP);
        glVertex2i(50,100);
        glVertex2i(50,85);
        glVertex2i(30,85); //decrease value increase feet
        glVertex2i(30,100);
    glEnd();

     //* draw eyes
    glBegin(GL_LINE_LOOP);
        glVertex2i(140,100);
        glVertex2i(140,105);
        glVertex2i(135,105);
        glVertex2i(135,100);
    glEnd();
    glFlush();


int main (int argc, char** argv)
    glutInit(&argc, argv);  // initialize the toolkit
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  // set display mode
    glutInitWindowPosition(100,150);  // set window position
    glutInitWindowSize(screenWidth,screenHeight); // set window size
    glutCreateWindow("House");  // create & open window
    glutDisplayFunc(drawBunny);  // register redraw function
    myInit();
    glutMainLoop();  // loop forever

【问题讨论】:

【参考方案1】:

编写一个由 2 个兔子组成的显示函数并旋转它们:

void display()

    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);

    drawBunny();

    glPushMatrix();
    glRotatef(degreetoreturn,x,y,z); // Adjust parameters according to what you need
    drawBunny();
    glPopMatrix();

    glutSwapBuffers();


从您的 drawBunny 函数中删除 glClear() 函数和 glFlush() 函数。最后在你的 main 函数中改变这一行:

glutDisplayFunc(drawBunny);  // register redraw function

glutDisplayFunc(display);  // register redraw function

【讨论】:

+1 - 但是有一个小问题:glutSwapBuffers (...) 在这种情况下不正确。像素格式是单缓冲的。虽然我不建议一开始就使用单缓冲像素格式 (GLUT_SINGLE),但这就是我们所拥有的:-\ @AndonM.Coleman 感谢您的警告,我阅读了您是对的文档。据说在单缓冲情况下,此功能无效。 昨晚玩了一晚上才发现问题。转换顺序是问题所在。 glTranslatef(100, 100, 0); glRotatef(180.0f,0.0f,0.0f,1.0f); glScalef(-1.0f,1.0f,1.0f); @Cagkan Toptas

以上是关于如何将计算机绘图旋转 90 度的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIImage 旋转 90 度?

css rotate(90deg)是旋转了90度还是旋转到90度

如何将包含图像的画布旋转 90、180、270 度?

给定一个 n × n 的二维矩阵表示一个图像, 将图像顺时针旋转 90 度js实现

如何将 N x N 矩阵旋转 90 度? [关闭]

如何在 ThreeJS 中将网格旋转 90 度?