OpenGL 2D 旋转问题

发布于 2024-11-06 01:19:15 字数 1266 浏览 1 评论 0原文

在下面的程序中,我试图画一个简单的房子。坐标在 house 数组中定义。我需要旋转房子并显示旋转后的房子和原始房子。 但为什么旋转后的房子没有显示出来呢?

    //Program to create a house like figure and rotate ir about a given fixed point using OpenGL functions.
#include <glut.h>
#include <stdio.h>

float house [11][2] = {{100,200},{200,250},{300,200},{100,200},{100,100},{175,100},{175,150},{225,150},{225,100},{300,100},{300,200}};

void init()
{
    glClearColor(1,1,1,0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0,800,0,800);
    glMatrixMode(GL_MODELVIEW);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    //NORMAL HOUSE
    glColor3f(1,0,0);
    glBegin(GL_LINE_LOOP);

    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();
    glFlush();


    //ROTATED HOUSE
    glPushMatrix();
    glRotatef(60,0,1,0);
    glColor3f(1,1,0);
    glBegin(GL_LINE_LOOP);

    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();
    glFlush();
    glPopMatrix();
}

void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("House rotation");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
}

In the following program i am trying to draw a simple house. The coordinates are defined in the house array. I need to rotate the house and display both rotated as well as the original house.
But why is the rotated house not being displayed?

    //Program to create a house like figure and rotate ir about a given fixed point using OpenGL functions.
#include <glut.h>
#include <stdio.h>

float house [11][2] = {{100,200},{200,250},{300,200},{100,200},{100,100},{175,100},{175,150},{225,150},{225,100},{300,100},{300,200}};

void init()
{
    glClearColor(1,1,1,0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0,800,0,800);
    glMatrixMode(GL_MODELVIEW);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    //NORMAL HOUSE
    glColor3f(1,0,0);
    glBegin(GL_LINE_LOOP);

    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();
    glFlush();


    //ROTATED HOUSE
    glPushMatrix();
    glRotatef(60,0,1,0);
    glColor3f(1,1,0);
    glBegin(GL_LINE_LOOP);

    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();
    glFlush();
    glPopMatrix();
}

void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("House rotation");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-11-13 01:19:16

尝试在 Z 轴而不是 Y 轴上旋转:

//Program to create a house like figure and rotate ir about a given fixed point using OpenGL functions.
#include <GL/glut.h>

float house [11][2] = {{100,200},{200,250},{300,200},{100,200},{100,100},{175,100},{175,150},{225,150},{225,100},{300,100},{300,200}};

void display()
{
    glClearColor(1,1,1,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,800,0,800);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //NORMAL HOUSE
    glColor3f(1,0,0);
    glBegin(GL_LINE_LOOP);
    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();


    //ROTATED HOUSE
    glPushMatrix();
    glTranslatef(100,100,0);
    glRotatef(60,0,0,1);
    glTranslatef(-100,-100,0);
    glColor3f(1,1,0);
    glBegin(GL_LINE_LOOP);
    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();
    glPopMatrix();
}

void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("House rotation");
    glutDisplayFunc(display);
    glutMainLoop();
}

编辑:这应该绕底角旋转。

Try rotating on the Z axis instead of the Y axis:

//Program to create a house like figure and rotate ir about a given fixed point using OpenGL functions.
#include <GL/glut.h>

float house [11][2] = {{100,200},{200,250},{300,200},{100,200},{100,100},{175,100},{175,150},{225,150},{225,100},{300,100},{300,200}};

void display()
{
    glClearColor(1,1,1,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,800,0,800);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //NORMAL HOUSE
    glColor3f(1,0,0);
    glBegin(GL_LINE_LOOP);
    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();


    //ROTATED HOUSE
    glPushMatrix();
    glTranslatef(100,100,0);
    glRotatef(60,0,0,1);
    glTranslatef(-100,-100,0);
    glColor3f(1,1,0);
    glBegin(GL_LINE_LOOP);
    for(int i=0;i<11;i++)
        glVertex2fv(house[i]);
    glEnd();
    glPopMatrix();
}

void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("House rotation");
    glutDisplayFunc(display);
    glutMainLoop();
}

EDIT: This one should rotate around the bottom corner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文