2022年3月15日 星期二

一定是大拇指的啦

 step 0 :下載模型

0.0  jsyeh.org/3dcg10 下載  data.zip  , windows.zip

0.1 data.zip =>下載/windows/data

     windows.zip => 下載/windows/Transformation.exe

step 1 :  旋轉

1.1 開啟 transformation.exe 透過模型練習 (右上角視窗可按右鍵更改模型)

1.2 轉軸方式 : 右手定理 ( 黑色的是旋轉方向 ,旋轉軸是右手大拇指)


1.3  可透過 glRotatef (角度 , x軸 , y軸 ,z軸 ) 先調整 xyz 軸再來轉換角度練習

step 2 : 旋轉茶壺

2.1 開啟opengl專案,命名為 week04_opengl_Rotate

2.2 貼上程式碼

#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glRotatef( 90,0,0,1);

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc , char**argv)

{

    glutInit( &argc , argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week04 Rotate");

    glutDisplayFunc(display);

    glutMainLoop();

}

step 3 : 滑鼠轉動

3.1 開新專案 命名為week04_mouse_motion

3.2貼上程式碼
#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();
}
void motion(int x, int y)
{
    angle=x; ///滑鼠左右拉動調整
    display();///重畫畫面
}
int main(int argc , char**argv)
{
    glutInit( &argc , argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04 Rotate");
    glutDisplayFunc(display);
    glutMotionFunc(motion); ///mouse motion動
    glutMainLoop();

}

step 4 : 滑鼠轉動後

4.0 因為按下滑鼠後,圖案會重新轉正在旋轉,所以要重設起始點

4.1 貼上程式碼
#include <GL/glut.h>
float angle=0 , oldX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef( angle,0,0,1);  
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle+=(x-oldX); ///新角度-改變後的起始角度
    oldX=x;  ///改變後的角度變成起始角度
    display();
}
void mouse ( int button , int state , int x ,int y)
{
    oldX=x;///固定
}
int main(int argc , char**argv)
{
    glutInit( &argc , argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04 Rotate");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);///mouse按下去、放開來
    glutMainLoop();

}
 step 5 : 額外補充
5.1 用小黑視窗點擊後保留點的位置並顯示圖案

5.2貼上程式碼

#include <GL/glut.h>
#include <stdio.h>
float angle=0 , oldX=0;
int mx[1000],my[1000]; ///備份程式碼位置
int n=0;///計算點擊次數
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_LINE_LOOP);
    for (int i=0;i<n;i++){ ///用迴圈畫出保留點形成圖案
        glVertex2f( (mx[i]-150)/150.0 , -(my[i]-150)/150.0);
    }
    glEnd();
    glutSwapBuffers();
}
void motion(int x, int y)
{
     angle+=(x-oldX);
    oldX=x;
    display();
}
void mouse ( int button , int state , int x ,int y)
{
   if ( state==GLUT_DOWN ){ ///滑鼠按下去的位置
        printf("   glVertex2f( (%d-150)/150.0 , -(%d-150)/150.0);\n",x,y);
        n++;///點擊次數增加    
        mx[n-1]=x;my[n-1]=y;  /// 備份點擊的x,y位置
    }
}
int main(int argc , char**argv)
{
    glutInit( &argc , argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04 Rotate");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutMainLoop();

}





沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...