2019年3月21日 星期四

金魚腦波學

電腦圖學

1.主題:旋轉Rotation
2.十座:glRotatef()
3.滑鼠轉 自動轉

右手旋轉定則

x=0,y=1,z=0(大拇指指向)

x=1,y=1,z=0(大拇指指向)

實作

自動旋轉的茶壺
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
     glRotatef(angle,0,1,0);今天新教的
     glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
     angle+=3;每執行這行就加一點點
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);IDLE閒閒沒事就重畫
    glutMainLoop();
}

用滑鼠控制茶壺轉動

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
     glRotatef(angle,0,1,0);
     glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}
void motion(int x,int y)
{
    angle=x;
    display();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

滑鼠按下去 彈起來

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
     glRotatef(angle,0,1,0);
     glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}
void motion(int x,int y)
{
    angle=x;
    display();
}
#include <stdio.h>
void mouse(int button,int state,int x,int y)
{
    printf("%d %d %d %d\n",button,state,x,y);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutMainLoop();
}









沒有留言:

張貼留言