2018年6月4日 星期一

WEEK04 QAQ筆記

WEEK04:移動、旋轉、縮放
                     複習上週:move 移動


任務一:Transformation.exe

datawin32glut32.dll 
解壓縮win32
glut32.dlldata放進wnidow資料夾

1.開啟Transformation.exe

2.按右鍵選取AI Capone,並拉動綠色的值,就可以調整模型的角度
(可參考"安培右手法則")


任務二:期中考

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();   //還原矩陣


任務三:畫出可自動旋轉的茶壺

1.自動旋轉茶壺












#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();
    printf("%.1f\n",angle);
    angle++; ///每次角度加1度
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("05160806_teapot_rotating");

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


2.手動旋轉茶壺(用滑鼠)











#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();
    angle++;   ///每次角度加一度
}
void mouse(int button, int state, int x,int y)
{

}
void motion(int x, int y)
{///Now:當mouse在drag motion時
    angle=x;   ///改變angle值
    glutPostRedisplay();
}
int main(int argc,char *argv[])
{
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutDisplayFunc(display);
    glutMainLoop();  
}


3.最終版 (用滑鼠轉起來比較順了)











#include <stdio.h>
#include <GL/glut.h>
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);
}
void mouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;
    if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0; 
    oldX=x;oldY=y;///Now2:按下去的時候,把oldX,oldY記起來!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
void motion(int x,int y)
{
    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);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week04");
    glutMouseFunc(mouse);//Now:mouse
    glutMotionFunc(motion);//Now:motion
    ///Now:glutIdleFunc(disolay);///很閒/Idle,去重畫
    glutDisplayFunc(display);///等一下樓上要準備 void display()
    glutMainLoop();
}

沒有留言:

張貼留言