2020年3月24日 星期二

week04 旋轉移動

Translate移動=glTranslatef(x,y,z)
Rotate旋轉=glRotatef(旋轉角度,以x軸,以y軸,以z軸)  
|   是x軸方向    一   是y軸方向    . 射出是 z軸方向    以安培右手定則轉
Scale大小=glScale(x,y,z)                                                                                                                       

const double a = myAngle;
////原本a自動 * 90 會不斷旋轉 這邊讓他 等於myAngle可以以下面函式操作
void motion(int x,int y)
{
    myAngle=x;
}
以x軸做旋轉
glutMotionFunc(motion);
呼叫函示

#include <GL/glut.h>float myAngle=0;///-1~+1的座標void display(){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景    glPushMatrix();///備份矩陣        glRotatef(myAngle,0,0,1);///對Z軸做旋轉        glutSolidTeapot( 0.3 );  ///實心茶壺    glPopMatrix();///備份矩陣    glutSwapBuffers(); ///交換兩倍buffer s}void motion(int x,int y){    myAngle =x;    display();///每次動作就要重複畫面}
int main(int argc, char**argv) ///高手等級的main(){    glutInit(&argc,argv);///main的參數,於近來    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);    ///啟動3D的顯示能力,兩倍顯示buffer,有3D深度值    glutCreateWindow("week04!!!");    ///建立3D窗子    glutMotionFunc(motion);///motion函式    glutDisplayFunc(display);    ///等一下怎麼畫    glutMainLoop();    ///主要迴圈卡在這裡,不會結束}


#include <GL/glut.h>
float myAngle=0;
void display(){   
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景   
glPushMatrix();///備份矩陣        
glRotatef(myAngle,0,0,1);///對Z軸做旋轉        
glutSolidTeapot( 0.3 );  ///實心茶壺   
glPopMatrix();///備份矩陣    
glutSwapBuffers(); ///交換兩倍buffer s
}
int oldx=0;
void mouse(int button,int state,int x,int y){    
if(state==GLUT_DOWN) 
oldx=x;///先記舊位置可以不用一直更新
}
void motion(int x,int y){    
myAngle +=(x-oldx);    
oldx=x;///更新角度   
 display();///每次動作就要重複畫面
}
int main(int argc, char**argv) ///高手等級的main(){   
 glutInit(&argc,argv);///main的參數,於近來   
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);    ///啟動3D的顯示能力,兩倍顯示buffer,有3D深度值    
glutCreateWindow("week04!!!");    ///建立3D窗子    
glutMotionFunc(motion);///motion函式    
glutMouseFunc(mouse);///moouse函式    
glutDisplayFunc(display);    ///等一下怎麼畫    
glutMainLoop();    ///主要迴圈卡在這裡,不會結束
}















沒有留言:

張貼留言