2020年3月25日 星期三

week04


用滑鼠移動glRotatefglRotatef裡的座標調整人物方向



更改程式碼變成用滑鼠移動圖片方向



讓茶壺可以旋轉


#include <GL/glut.h>
#include <stdio.h>
float myAngle=0;
void motion(int x,int y)
{
    myAngle=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngle,0,0,1);
        glutSolidTeapot( 0.3 );

    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}



選轉茶壺MAX進化版


#include <GL/glut.h>
#include <stdio.h>
float myAngle=0,oldx=0;
void motion(int x,int y)
{
    myAngle+=(x-oldx);
    oldx=x;
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    oldx=x;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngle,0,0,1);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutMainLoop();
}

沒有留言:

張貼留言