2018年3月19日 星期一

Week04 立欣的筆記

01.
glRotatef(角度,x軸,y軸,z軸)
                           旋轉軸




02.

期中考題:

glPushMatrix();   //備份矩陣
   glTranslatef(x,y,z);   //移動
   glRotatef(angle,x,y,z);   //轉動
   glScalef(x,y,z);   //放大縮小
   glBegin(GL_POLYGON);   //開始畫
      glColor3f(r,g,b);   //顏色
      glVertex3f(x,y,z);   //頂點
   glEnd();   //結束畫 glBegin()對應
glPopMatrix();   //還原矩陣

03.

畫出自動旋轉的茶壺

#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    printf("%.1f\n",angle);
    angle++;   ///每次角度加1度
}

int main(int argc,char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");


    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}



04.

加入手動旋轉

#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);   ///兩種模式
    glPushMatrix();   ///備份矩陣
        glRotatef(angle,0,0,1);   ///旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();   ///還原矩陣
    glutSwapBuffers();   ///交換畫面buffer便會顯示
    angle++;   ///每次角度加一度
    ///Now:printf("%.1f\n",angle); ///Now:angle++; ///每次角度加1度
}
void mouse(int button, int state, int x,int y)
{

}
void motion(int x, int y)
{///Now:當mouse在drag motion時
    angle=x;   ///Now:去改變angle值
    glutPostRedisplay();   ///Now:請畫面重畫
}
int main(int argc,char *argv[])   ///進階的main()參數(個數,字串)
{
    glutInit(&argc, argv);   ///進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);   ///開始2種模式
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);   ///Now: mouse
    glutMotionFunc(motion);   ///Now: motion
    ///Now:glutIdleFunc(display);   ///很閒Idle,去重畫
    glutDisplayFunc(display);   ///等一下樓上要準備 void display()
    glutMainLoop();  ///主要迴圈
}



05.

加入移動的旋轉茶壺

#include <stdio.h>
#include <GL/glut.h>///使用GLUT外掛
float angle=0,teapotX=0,teapotY=0;
int nowRotate=0,oldY=0,oldX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///2種模式
    glPushMatrix();///備份矩陣
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);///旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();///交換畫面buffer便會顯示
    ///Now:printf("%.1f\n",angle);///Now:angle++;//每次角度加1度
}
void mouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;///Now2:如果是左鍵,就旋轉
    if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0; ///Now2:如果是右鍵,就移動
    oldX=x;oldY=y;///Now2:按下去的時候,把oldX,oldY記起來!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
void motion(int x,int y)
{///Now:當mouse在drag motion時
    if(nowRotate==1) angle+=(x-oldX);///Now:取改變angle值///Now2:加上改變量
    else{
                 teapotX+=(x-oldX)/150.0;///Now2:加上改變量!!!!!!!!!!!!!!
                teapotY+=(oldY-y)/150.0;///Now2:加上改變量!!!!!!!!!!!!!!

    }
    oldX=x;oldY=y;///Now2:做完計算時把oldX,oldY記起來!!!!!!!!!!
    glutPostRedisplay();///Now:請畫面重畫
}
int main(int argc, char**argv)///進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);///進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);///開始2種模式
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);//Now:mouse
    glutMotionFunc(motion);//Now:motion
    ///Now:glutIdleFunc(disolay);///很閒/Idle,去重畫
    glutDisplayFunc(display);///等一下樓上要準備 void display()
    glutMainLoop();///主要迴圈
}

沒有留言:

張貼留言